Reputation: 3
My password checker is working however it resolves only when the user input is the same as the entire .txt file.
How can I put multiple passwords in the .txt file and have my program work if any of them match the input? I would like to be able to be able to add the password 123456 so that my second if statement will work.
#simple program to check passwords against a txt file database passwordfile = open('secretpasswordfile.txt') secretpassword = passwordfile.read() print('Enter your password.') typedpassword = input() if typedpassword == secretpassword: print('Access granted.') if typedpassword == '123456': print('This password is not secure.') else: print('Access denied.')
the secretpasswordfile.txt only has genericpassword written in it.
Upvotes: 0
Views: 555
Reputation: 7020
Assuming every password in your file is separated by a newline, you can check if any of them match with this code. It uses the fact that you can treat the file object returned by open
as an iterator for each line in the file and compares the typed password to each of these lines. the .strip()
is to pull off the trailing newline from each line.
passwordfile = open('secretpasswordfile.txt')
print('Enter your password.')
typedpassword = input()
if any(typedpassword == pw.strip() for pw in passwordfile):
print('Access granted.')
if typedpassword == '123456':
print('This password is not secure.')
else:
print('Access denied.')
Upvotes: 1