我正在努力练习,因为我在学校做了未经审核的评估,而且我的代码仍然存在,但它没有按预期工作,因为它没有无论我写的是什么,都说用户名是正确的:/
acc = input("Do you have an existing account? y/n ")
if acc == "y":
accnick= input("Enter your username. ")
while accnick in open("file.txt").read():
print("Correct username.")
accpass= input("Enter your password. ")
if len(accpass) == 0:
accpass = input("Try again. ")
while accpass in open("file.txt").read():
print("Correct password. ")
break
else:
accnick = input("Wrong username, try again. ")
elif acc == "n":
name = input("Enter your name. ")
while len(name) == 0:
name = input("You haven't entered anything, try again. ")
age = int(input("Enter your age. "))
age = str(age)
while len(age) == 0:
age = input("Enter your age again. ")
password = input("Enter your password. ")
nick = name[:3]
nickname = nick+str(age)
file=open("file.txt","w+") #w+ is to write to a file and create a file if it doesnt exist yet
file.write(name+' '+str(age)+' '+nickname+' '+' '+password)
file=open("file.txt","r")
kurwa=file.read()
print(kurwa)
name = input("Enter your name. ") ###
if 'Test Test' in open('file.txt').read():
print("Someone with that name already exists. ")
答案 0 :(得分:0)
在您的代码中,在输入正确的用户名后,它将跳回print("Correct username.")
。因此,您将陷入无限循环。您的break
应该是while循环的一部分。此外,我将第二个while
更改为if
。
试试这个:
acc = input("Do you have an existing account? y/n")
if acc == "y":
accnick= input("Enter your username. ")
while accnick in open("file.txt").read():
print("Correct username.")
accpass= input("Enter your password. ")
if len(accpass) == 0:
accpass = input("Try again. ")
if accpass in open("file.txt").read():
print("Correct password. ")
break