我在菜单系统的某些代码中遇到了一些问题。到目前为止,我没有完成但我遇到了问题。它可以和account.leeman一起工作,但我永远不会得到'管理员'的工作,它总是回滚到开始。我已经看了一段时间的代码,但我无法弄清楚为什么这是为什么它永远不会继续。 “添加”的部分代码可以忽略不计,因为它不是一个问题还没有,但如果你能改善它看到任何方式,请让我知道Python 3中的循环
所以这是我:
usernames=["m.leeman","administrator"]
passwords=["pA55w0rd","password"]
while True:
count=0
add_or_enter=input("Would you like to Enter a username or Add a new one?: ").lower()
if add_or_enter=="enter":
username=input("Please enter username: ")
while (count+1)!=(len(usernames)):
#problem somewhere here
if username==usernames[count]:
print("Username accepted")
password=input("Please enter password: ")
if password==passwords[count]:
print("Welcome "+str(username))
print("Continue here")
else:
print("Incorrect password")
else:
count+=1
if count==len(usernames):
print("User does not exit")
else:
()
#should run again after this
elif add_or_enter=="add":
new_user=input("Enter a new username: ")
if (new_user=="add") or (new_user==usernames[count]):
print("Username unavailiable")
else:
count=+1
if count==len(usernames)-1:
usernames.append(new_user)
new_password=input("Enter a password for this user: ")
passwords.append(new_password)
print("User "+new_user+" successfully created")
任何回复将非常有帮助。谢谢。
什么是理想的行为? – roganjosh
假设,如果你做'while count!= len(username):'会发生什么? – Kevin
似乎在用户输入中可能存在一些来自'raw_input''用户名'和''m.leeman \ n“'的隐藏换行符,在你指出''#problem某处在''之后'的行后,尝试'if username.strip()==用户名[count] .strip():'清理字符串并剥离换行符。工作时,我试了一下。 – davedwards