End-To-End Example: Bad Password Checker

  • Read in list of bad passwords from file bad-passwords.txt
  • Main program loop which:
    • inputs a password
    • checks whether the password is "good" or "bad" by checking against the list
    • repeats this until you enter no password.

In [1]:
# read passwords into list
#todo:
#input: none, output: list of bad passwords
# for each line in bad-passwords.txt
# add to list

def read_passwords():
    bad_password_list = []
    filename = "ETEE-bad-passwords.txt"
    with open(filename) as f:
        for line in f:
            bad_password_list.append(line.strip())
    return bad_password_list

# password in list?
#input: password list and a password to check, output: True or False
#todo
# get index of password in list
# return true
# when ValueError return false

def password_in_list(password, bad_password_list):
    try:
        index = bad_password_list.index(password)
        return True
    except ValueError:
        return False

# main program 
bad_password_list = read_passwords()
print("This program will check for quality passwords against a list of known bad passwords.")
while True:
    password = input("Enter a password or ENTER to quit: ")
    if password == "":
        break
    if password_in_list(password, bad_password_list):
        print("%s is a bad password. It is on the list." % (password))
    else:
        print("%s seems like an ok password. It is not on the list." % (password))


This program will check for quality passwords against a list of known bad passwords.
Enter a password or ENTER to quit: 123456
123456 is a bad password. It is on the list.
Enter a password or ENTER to quit: fjdskafjoda;shv
fjdskafjoda;shv seems like an ok password. It is not on the list.
Enter a password or ENTER to quit: test
test is a bad password. It is on the list.
Enter a password or ENTER to quit: pasword
pasword seems like an ok password. It is not on the list.
Enter a password or ENTER to quit: password
password is a bad password. It is on the list.
Enter a password or ENTER to quit: 

In [ ]: