简体   繁体   English

Python登录恢复系统出现问题

[英]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. 文本文件中的格式为: username: (username) password: (password)由于某种原因,当我输入帐户的用户名时,会为其提供密码,但由于某种原因,它会在末尾说Username not found而我却Username not found不知道该如何解决。

After check = False , you'll have to add break . check = False ,您必须添加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. 另外,由于check变为False ,因此我们可以在循环完成后进行检查。 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: 输入: 输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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