-1

I am currently writing a Log-in code in Python. I am working on password strength detecting, very basic stuff, using if statements and the all() function. I'm not sure why, but when I run this code, I doesn't detect the absence of uppercase characters in pswrd, yet does detect digits. This is my first stack overflow post and I was wondering if any could help me out. This is my test code:

import string
n = 1
def spv(password):
    global n
    punct = string.punctuation
    pswrd = password
    print(punct)
    if len(pswrd) < 8:
        print("Password must be over 7 charcters long!")
    
        
    elif all(not char in punct for char in pswrd) :
        print("Password must contain a special character. !?, ... etc")
    
        
    elif all(not char.upper() for char in pswrd):
        print("Password must contain at least one UPPERCASE letter!")

            
    elif all(not char.isdigit() for char in pswrd):
        print("Password must contain a number!")
    else:    
        print("Password is Strong!")
        n-=1
        return n
    
        
while n != 0:
    word = input("Write password: ")
    spv(word)
    if n == 0:
        print("Working")`

2 Answers2

1

try [char.isupper() for char in pswrd]

upper method returns string. bool function returns True for any non-empty string

falm
  • 51
  • 1
  • 6
  • but i guess, it in not good solution. in case password contains uppercase letters only, it will pass password. I think, string password should contain at least one uppercase and one lowercase – falm Sep 06 '22 at 12:51
  • Well in that case I just add another if statement with .islower() – Programming_Noob Sep 07 '22 at 13:17
  • https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a – falm Sep 08 '22 at 15:04
1

If you change;

elif all(not char.upper() for char in pswrd):

to;

elif all(not char.isupper() for char in pswrd):

Your code will work just fine.

MaxK
  • 73
  • 7