Python登录恢复系统出现问题

时间:2019-01-06 14:22:16

标签: python python-3.x

这是用户可以恢复其用户名或密码的系统

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")

文本文件中的格式为:username: (username) password: (password)由于某种原因,当我输入帐户的用户名时,会为此输入密码,但是由于某种原因,它会在末尾Username not found上说,但我没有不知道该如何解决。

1 个答案:

答案 0 :(得分:1)

check = False之后,您必须添加break。这是因为循环不断进行,导致出现“未找到用户名”打印。另外,由于check变成了False,因此我们可以在循环完成后进行检查。代码为:

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