# File: validPassword.py # Author: Dr. Gibson # Date: 2/22/2017 # Section: N/A # E-mail: k.gibson@umbc.edu # Description: # This file contains python code that asks the user to # enter a password, with specific requirements needed # in order for the password to be valid MIN_LEN = 8 # minimum length of password MAX_LEN = 20 # maximum length of password BAD_CHAR1 = " " # disallowed character #1 BAD_CHAR2 = "_" # disallowed character #2 def main(): #set up the boolean flag passwordValid = False # this will evaluate to True as long as the password is NOT valid while passwordValid == False: # get the password and assume (for now) that it's valid password = input("Please enter a password: ") passwordValid = True # if the password is too short, it's invalid if len(password) < MIN_LEN: print("Your password is too short.") passwordValid = False # if the password is too long, it's invalid if len(password) > MAX_LEN: print("Your password is too long.") passwordValid = False # to check for the disallowed characters, we need to iterate # over the entire list, by looking at each element (by index) place = 0 while place < len(password): # if they have the first bad character (space), it's invalid if password[place] == BAD_CHAR1: print("You can't have any", BAD_CHAR1, "in your password") passwordValid = False # if they have the second bad character (underscore), it's invalid if password[place] == BAD_CHAR2: print("You can't have any", BAD_CHAR2, "in your password") passwordValid = False place += 1 # if none of these if conditionals was true, then passwordValid # will still be True, and the while loop will exit # exited the while loop, so print out the chosen password print("Thanks for picking the super-secure password", password) main()