简体   繁体   中英

problem with Python login recovery system

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.

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: 结果

Input: 输入

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM