Ben
Ben

Reputation: 105

Python Conditional Password Program

I'm writing a simple conditional password checker script with the conditions being:

I have all the code typed out but it isn't preforming quite like I need it to. For instance, if I type "FunkyFunky56" It throws out the message that it must contain two uppercase letters when it clearly does. However, when I type it out with two consecutive uppercase letters, "FUnkyfunky56", it gives me the valid password message. Can anyone help me to understand what I need to change?

Code:

import string

def longPass(password):
    'Password must be at least 8 characters'
    return len(password) > 8

def shortPass(password):
    'Password cannot be more than 24 characters'
    return len(password) < 24

def lowerPass(password):
    'Password must contain at least two lowercase letters'
    return len(set(string.ascii_lowercase).intersection(password)) > 1

def upperPass(password):
    'Password must contain at least two uppercase letters'
    return len(set(string.ascii_uppercase).intersection(password)) > 1

def numPass(password):
    'Password must contain at least two digits'
    return len(set(string.digits).intersection(password)) > 1

def specPass(password):
    'Password must not contain any special characters'
    return len(set(string.punctuation).intersection(password)) < 1

def test_password(password, tests=[longPass, shortPass, lowerPass, upperPass, numPass, specPass]):
    for test in tests:
        if not test(password):
            print(test.__doc__)
            return False
    return True

def main():
    password = input('Please enter a password to be checked: ')
    if test_password(password):
        print('Valid password!')

if __name__=="__main__":
    main() 

Upvotes: 0

Views: 1136

Answers (1)

viraptor
viraptor

Reputation: 34145

That's because if it contains two "F"s, the set will only contain one of them anyway. As implemented now, you're requiring at least two different uppercase characters.

Try simply counting the matching characters instead of doing set intersections.

This will do the correct counting of uppercase chars for example: sum(x.isupper() for x in password)

Upvotes: 1

Related Questions