Home > database >  all() function is Python - Works for digits, not for uppercase?
all() function is Python - Works for digits, not for uppercase?

Time:09-06

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")`

CodePudding user response:

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.

CodePudding user response:

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

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

  • Related