0

This Is a system where the user can recover their username or password

account = input("What do you want to recover? Username or Password? ")
if account == ("Password") or account == ("password"):
    check = True
    while check:
        username = input("Enter your username for your account ")
        with open("accountfile.txt","r") as file:
            for line in file:
                text = line.strip().split()
                if username in text:
                    print(line)
                    check = False
                else:
                    print("Username not found")

The format in the text file is: username: (username) password: (password) For some reason when I enter the username for the account it gives the password for it but for some reason it says at the end Username not found and I don't know how to fix this.

2
  • 1
    You need to exit from the for loop. It prints the username, then the next evaluation comes, and then the username is not there, so it prints Username not found. Isn't this the issue? Commented Jan 6, 2019 at 14:31
  • 1
    put in a "break" after check = False Commented Jan 6, 2019 at 14:32

1 Answer 1

1

After check = False, you'll have to add break. This is because your loop keeps on going for every line, causing the "No Username Found" print. Also, since check becomes False, we can check this after the loop is done. The code would be:

account = input("What do you want to recover? Username or Password? ")
if account == ("Password") or account == ("password"):
    check = True
    while check:
        username = input("Enter your username for your account ")
        with open("accountfile.txt","r") as file:
            for line in file:
                text = line.strip().split()
                if username in text:
                    print(line)
                    check = False
                    break
            if (check == True):
                print("Username not found")

Results: Results

Input: Input

Sign up to request clarification or add additional context in comments.

3 Comments

This is only a partial answer. The script will keep printing "Username not found" until it finds a match – that is, if the username is on line 42, you'll see 41 lines saying "Username not found".
@JJJ Yep you were right I put other users into the file and tested it and it sthttps://stackoverflow.com/questions/54062441/problem-with-python-login-recovery-system/54063989#comment94963389_54063989ill sayes username not found do you know a permanent fix?
Check edit. Just fixed it. @python987 Also, added input and results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.