Using a loop to read a file and search for content, allow user to input multiple times to find a match

I have a text file that contains 100 passwords. I want the user to be able to input their password, and have the program check the file. If the password entered matches then the program can end, but if the password does not match I want the to be able to try again.

pwEntry = input("Enter your password: ")

f1 = open('passwords.txt','r')
for line in f1:
    if pwEntry in line:
        print("success")
    else:
        print("try again")
f1.close()

I have tried a while loop, but it just says try again every time, even if I put in a correct password so its broken somehow.

f1 = open('passwords.txt','r')
for line in f1:
    line = line.rstrip()
pwEntry = 'x'
while pwEntry not in f1.read():
    pwEntry = input("Enter your password: ")
    if pwEntry in f1.readlines():
        print("Success")
    else:
        print("Try again")


Read more here: https://stackoverflow.com/questions/64947722/using-a-loop-to-read-a-file-and-search-for-content-allow-user-to-input-multiple

Content Attribution

This content was originally published by Nicole at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: