Python 尽管输入了正确的信息,程序总是求助于“错误登录”
我看不到我的代码中有任何总是导致输出错误登录的缺陷Python 尽管输入了正确的信息,程序总是求助于“错误登录”,python,Python,我看不到我的代码中有任何总是导致输出错误登录的缺陷 credentials = { "username1": "password1", "username2": "password2" } while True: useroutput = input("Type in your username: ") passoutput = input("Type i
credentials = {
"username1": "password1",
"username2": "password2"
}
while True:
useroutput = input("Type in your username: ")
passoutput = input("Type in your password: ")
if useroutput in credentials.keys() and not(passoutput in credentials.keys()) and passoutput in credentials:
print("Welcome back "+useroutput+"!")
break
else:
print("Wrong login!")
continue
这将检查用户名是否已知,然后检查密码是否分配给用户名
credentials = {
"username1": "password1",
"username2": "password2"
}
while True:
useroutput = input("Type in your username: ")
passoutput = input("Type in your password: ")
if useroutput in credentials:
if credentials[useroutput] == passoutput:
print("Welcome back "+useroutput+"!")
break
print("Wrong login!")
你的情况不起作用,因为这部分
not(passoutput in credentials.keys()) and passoutput in credentials
永远都是假的。凭证中的passoutput将签入密钥。我想您希望在credentials.values中使用passoutput。即使这个“会”起作用,每个用户都可以使用字典中的每个密码登录。如果条件没有意义,那么我完全可以理解,请详细说明为什么不在凭据中同时使用notpassoutput.key和passoutput凭据?这自相矛盾。