这是用户可以恢复其用户名或密码的系统
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
上说,但我没有不知道该如何解决。
答案 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")