Home > Net >  How do I condense a failed list of criteria with return False for the password validation?
How do I condense a failed list of criteria with return False for the password validation?

Time:09-15

def isValidPassword(password):
    if (len(password)<=8):
        print("Password must be at least 8 characters or more.")
        return False
    if any(digit.isdigit() for digit in password):
        print("Password must have at least single digit or more.")
        return False
    if any(digit.isupper() for digit in password):
        print("Password must have at least one uppercase letter or more.")
        return False
    if any(digit.islower() for digit in password):
        print("Password must have at least one lowercase letter or more.")
        return False
    return True

def confirmedPassword():
    isSecure = False ; isMatch = False
    password = "" ; reenterPassword = ""
    
    while(isSecure == False): 
        password = input("Enter your password: ")
        isSecure = isValidPassword(password)
    while(isMatch == False):
        reenterPassword = input("\nPlease re-enter your password: ")
        if (password == reenterPassword):
            isMatch = True ; print("Password is confirmed. Thank you.")
            print("The password you entered is approved and safe.")
        else:
            (password == reenterPassword)
            isMatch = False ; print("Password is not confirmed. Please try again.")
confirmedPassword()

If anyone can please help. It is appreciated. I had a hard time figuring out how to condense a failed list of criteria printed out when I input "ABC" to run the program.

CodePudding user response:

I changed your code to print out all the issues with the password before returning whether or not it is a valid password. I also fixed all of the if statements you had to correctly capture the password issue.

def isValidPassword(password):
    valid = True
    if (len(password) < 8):
        print("Password must be at least 8 characters or more.")
        valid = False
    if not any(digit.isdigit() for digit in password):
        print("Password must have at least single digit or more.")
        valid = False
    if not any(digit.isupper() for digit in password):
        print("Password must have at least one uppercase letter or more.")
        valid = False
    if not any(digit.islower() for digit in password):
        print("Password must have at least one lowercase letter or more.")
        valid = False
    
    return valid

def confirmedPassword():
    isSecure = False ; isMatch = False
    password = "" ; reenterPassword = ""
    
    while(not isSecure): 
        password = input("Enter your password: ")
        isSecure = isValidPassword(password)
    while(not isMatch):
        reenterPassword = input("\nPlease re-enter your password: ")
        if (password == reenterPassword):
            isMatch = True
            print("Password is confirmed. Thank you.")
            print("The password you entered is approved and safe.")
        else:
            isMatch = False
            print("Password is not confirmed. Please try again.")
confirmedPassword()
  • Related