0

So here is the issue. I have a series of functions with which an inputString from the user gets checked to meet all of the set password criteria:

  • Passwords must be at least 5 characters long
  • Passwords must contain at least one upper case letter
  • Passwords must contain at least two numbers
  • Passwords may not contain the characters "E" or "e"
  • Passwords must include at least one non-alphanumeric character.
  • A password may not be a frequently used password: 'password', '12345', 'qwerty', 'letmein', 'trustno1', '000000', 'passw0rd,'Password'

My last function attempts to collect all of the functions defined into a single usable module function. There are no errors running the program but there is a bug which always prints, "Invalid! Password must contain special character." Why is that so? And what other bugs or fixed do you guys suggest to make this code more efficient or readable?

def isFiveCharacters(inputString):
    while len(inputString) > 5:
        return True #print('Contains at least 5 characters, ')
    else:
        print('Invalid! Password must cantain more than 5 characters')
    return False
def hasUpperCase(inputString):
    x = any(char.isupper() for char in inputString)
    if x == True:
        return True #print ('an uppercase letter, ')
    if x == False:
        print('Invalid! Password must contain an upper case letter')
    return False
def hasNumbers(inputString):
    count = 0
    for char in inputString:
        if char == char.isdigit():
            count += 1
            if count >= 2:
                #print ('two numbers, ')
                return True
            elif count < 2:
                print ('Invalid! Password must contain two numbers')
                return False
def hasLetterE(inputString):
    for char in inputString:
        if 'E' and 'e' in inputString:
            print('Invalid! Password cannot contain the letter "E"')
            return False
        else:
            #print('does not contain the letter E, ')
            return True
    #if 'e' in inputString:
#        print('Password cannot contain the letter "e"')
#    return None
def nonAlphaNumChar(inputString):
    special_char = ['!','@','$','%','#','^','&','*']
    if inputString == special_char * 2:
            #print('a special character, ')
        return True
    else:
        print('Invalid! Password must contain a special character')
    return None
def usedPasswords(inputString):
    used_passwords = ('password','12345','qwerty','letmein','trustno1','000000','passw0rd','Password')
    if used_passwords == inputString:
        print('Invalid! Password must be original.')
        return False
def passwordCriteria(inputString):
    isFiveCharacters(inputString)
    hasUpperCase(inputString)
    hasNumbers(inputString)
    hasLetterE(inputString)
    nonAlphaNumChar(inputString)
    usedPasswords(inputString)
    while inputString == True:
        print('Valid Password')
        return True
    if inputString == False:
        print('Error, invalid password')
        return False
    return None
wp78de
  • 18,207
  • 7
  • 43
  • 71

1 Answers1

0

I am just going to point out the obvious mistake:

You should collect the values returned by the functions like this

valid = isFiveCharacters(inputString)
# then just use the boolean values with an `and`
valid = valid and hasUpperCase(inputString)
valid = valid and hasNumbers(inputString)
# and so on
# then use
if valid:
    print("Valid Password")
else:
    print("Invalid Password")

I suggest reading about functions the return statement and the while loop in detail and getting a clear understanding of how they work.

Arunmozhi
  • 1,034
  • 8
  • 17