这是一个用户可以在其中恢复其用户名或密码的系统
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,而我不知道如何解决这个问题。
发布于 2019-01-07 01:13:09
在check = False之后,您必须添加break。这是因为您的循环将继续执行每一行,从而导致打印"No Username Found“。此外,因为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")结果:

输入:

https://stackoverflow.com/questions/54062441
复制相似问题