Trouble running a password validation while loop with python

Viewed 350

I'm very new to python and I'm trying to use a while loop that will validate a password based on several criteria but ends one all the criteria is met. Can any one give me some tips on how to make this work? I've tried using an else statement but for some reason it wont allow it.

def chk_password():

#Display info about password requirements
print('\n\nYou need a new password')
print('It must have at least six characters, but not more than 15')
print('It cannot contain any variation of \"umgc\"')
print('It cannot contain spaces')
print('It must contain the \"*\" character\n')

password = input('\nPlease enter your password:\n')

def chk_minlength():
    if len(password) >= 6:
        return True
    else:
        return False
def chk_maxlength():
    if len(password) <= 15:
        return True
    else:
        return False
def chk_spaces():
    if ' ' in password:
        return False
    else: 
        return True
def chk_specchar():
    if '*' in password:
        return True
    else:
        return False
def chk_umgc():
    lower_password = password.lower()
    if 'umgc' in lower_password:
        return False
    else:
        return True

chk_minlength()
chk_maxlength()
chk_spaces()
chk_specchar()
chk_umgc()

while chk_minlength == False or chk_maxlength == False or chk_spaces == False or chk_specchar == False or chk_umgc == False:
    print('Your password was invalid. Please make the following corrections:')          
    if chk_minlength == False:
        print('Your password must have at least six characters')
    if chk_maxlength == False:
        print('Your password cannot have more than 15 characters')
    if chk_spaces == False:         
        print('Your password cannot any spaces')
    if chk_specchar == False:
        print('Your password must contain the \"*\" character.')
    if chk_umgc == False:
        print('Your password cannot contain any variation of \"umgc\"')
    password = input('\nPlease enter your password:\n')
    if chk_minlength == True and chk_maxlength == True and chk_spaces == True and chk_specchar == True and chk_umgc == True: 
        print('Your password met all the requirements. Thank you.')
        break

chk_password()

3 Answers

This looks mostly correct; however you are never saving the return values of your chk_* methods. In the condition of your while loop you are checking if chk_minlength == True or ..., which is checking if the value of the method definition is True not if the return value is True. To fix this you can simply us the method calls rather than reference to the methods:

while not (chk_minlength() or chk_maxlength() or chk_spaces() or chk_specchar() or chk_umgc()):
    pass

Since you reuse the values inside the while loop, it makes more sense here to store the values beforehand:

valid_min_length = chk_minlength()
valid_max_length = chk_maxlength()
no_spaces = chk_spaces()
no_specchar = chk_specchar()
no_umgc = chk_umgc()

while not (valid_min_length or valid_max_length or no_spaces or no_specchar or nor_umgc):
   pass

Try this:

def check_password(password):
    if len(password) <=6 or len(password) >=15: # Checks if password is between 6 and 15 charecters long
        return False
    if (' ' in password) or ('\n' in password) or ('*' in password): # Checking if there are any spaces, asteriks, or newlines are in the password
        return False

    if 'UMGC' in password.upper(): # Checking if the string 'UMGC' is in the password
        return False

    return True
    '''
    Since a return function will automatically exit the function,
    the only way for this line to be reached is if all the other if statements are not satisfied,
    meaning that your password is safe
    '''

print(check_password("Hello World"))
print(check_password("Hello"))

What we are doing is essentially going through each of your functions in if-statements. Then, if any of those if statements have been satisfied, it will return a False value. Otherwise, it will have to return a True value. I recommend you add something to check if there are any numbers or uppercase characters in the password for practice.

#Display info about password requirements
print('\n\nYou need a new password')
print('It must have at least six characters, but not more than 15')
print('It cannot contain any variation of \"umgc\"')
print('It cannot contain spaces')
print('It must contain the \"*\" character\n')

password = input('\nPlease enter your password:\n')

def chk_minlength():
    return len(password) < 6
def chk_maxlength():
    return len(password) > 15
def chk_spaces():
    return ' ' in password
def chk_specchar():
    return not '*' in password
def chk_umgc():
    return 'umgc' in password.lower()

while chk_minlength() or chk_maxlength() or chk_spaces() or chk_specchar() or chk_umgc():
    print('Your password was invalid. Please make the following corrections:')          
    if chk_minlength():
        print('Your password must have at least six characters')
    if chk_maxlength():
        print('Your password cannot have more than 15 characters')
    if chk_spaces():         
        print('Your password cannot any spaces')
    if chk_specchar():
        print('Your password must contain the \"*\" character.')
    if chk_umgc():
        print('Your password cannot contain any variation of \"umgc\"')
    password = input('\nPlease enter your password:\n')
print('Your password met all the requirements. Thank you.')

edit: fixed a syntax error :/

Related