im试图用python编写代码,基本上是这样,在终端中,您可以输入是否要注册或登录(如果您已经注册(我真的不知道如何将注册输入存储在字典中,然后当你尝试输入用户名和密码时,它会确保与注册功能中的用户名和密码相同提前感谢
到目前为止,我试过
accounts = {"user":"password", "user2":"password2"}
login_or_signup = input("Login or signup? ")
if login_or_signup.upper() == 'LOGIN':
username = input("Enter your username: ")
if username in list(accounts.keys()):
password = input("Enter your password: ")
if password in list(accounts.values()):
print("Logged in successfully.")
else:
print("Account credentials do not match.")
else:
print("Account not found.")
elif login_or_signup.upper() == "SIGNUP":
username = input("Enter your username: ")
password = input("Enter your password: ")
accounts.update({user,password})
但是我收到一个错误
您的最后一行有一个拼写错误,应该是username
而不是user
。也是:
而不是,
accounts.update({username: password})
这将使你的程序不会在两个选项中都失败。
然而,首先,您没有正确检查密码,您需要获取用户并检查密码是否与字典中的用户对应。
此外,您的程序将在您设置新注册后立即完成。您可以在顶部添加while True
循环,并在您想要的条件下相应地添加break
(示例(
您应该检查输入的密码是否与输入的用户名匹配。
accounts = {"user": "password", "user2": "password2"}
login_or_signup = input("Login or signup? ")
while True:
if login_or_signup.upper() == 'LOGIN':
username = input("Enter your username: ")
if username in accounts:
password = input("Enter your password: ")
if accounts[username] == password:
print("Logged in successfully.")
else:
print("Account credentials do not match.")
else:
print("Account not found.")
elif login_or_signup.upper() == "SIGNUP":
username = input("Enter your username: ")
password = input("Enter your password: ")
accounts[username] = password