0

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")
Nicole
  • 17
  • 1
  • 6
  • That's great, what is your question about this? – mkrieger1 Nov 21 '20 at 20:32
  • When I run this code it does not go back to the top to get the user input again without me starting the program over, which I am not allowed to do. I cannot figure out how to add a loop so that it keeps asking for user input until they get a matching password. – Nicole Nov 21 '20 at 20:49
  • 1
    This should answer why your second attempt doesn't work correctly: https://stackoverflow.com/questions/3906137/why-cant-i-call-read-twice-on-an-open-file – mkrieger1 Nov 21 '20 at 21:05

3 Answers3

1

When f1.read() is called, EOF (End-Of-File) is reached. In order to call successfully the function in every loop to read the file you should use seek(size) function to reset the file pointer in the beginning of the file.

Specifically:

f1 = open('passwords.txt','r')

while True:
    pwEntry = input('Enter your password: ')
    if pwEntry not in f1.read():
        print('Invalid password')
        f1.seek(0)  # Resets file reading in position 0 (beginning)
        continue
    break
        

Take a look at python docs as well.

lezaf
  • 432
  • 2
  • 9
0
f1 = open('passwords.txt','r')

password = []    #Creating Empty List

for line in f1:
    password.append(line.rstrip())   #Storing all password in the list

#print(password) 

while True:       #Running the while loop infintely till condition is achieved
    pwEntry = input("ENTER PASSWORD : ")
    if pwEntry in password:     #Using 'in' keyword to search enter password in list
        print("SUCCESS")
        break       #Breaking out of loop
    else:
        print("INVALID")

        
f1.close()

In order to rectify the problem print the list and observe the password, the presence of escape sequence may alter your result like '\t', '\n' , etc,.

0

OK I think I fixed it! Thank you!

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: ")
    f1.seek(0)
    if pwEntry in f1.read():
        print("Success")
        break
    else:
        print("Try again")
Nicole
  • 17
  • 1
  • 6