简体   繁体   English

您可以将变量添加到字典中吗

[英]Can you add variables into a dictionary

I would like to add a shelf to a program and can't seem to add two variables to the dictionary. 我想在程序中添加一个架子 ,但似乎无法在字典中添加两个变量。 For example, a username and password that the user inputs. 例如,用户输入的用户名和密码。 Is this possible? 这可能吗? And if so, how could I link them to each other? 如果是这样,我如何将它们彼此链接?

details=shelve.open['user_details']
details['Name']=username
details['Password']=password

So when the user wants to log in I can check if their details are matching, along the lines of: 因此,当用户想要登录时,我可以按照以下方式检查其详细信息是否匹配:

if username and password in details:
    user is logged in

username and password in details: is not really a proper boolean statement, at least it won't do what you think that does. username and password in details:并不是真正的布尔语句,至少它不会做您认为做的事情。 You are asking if the variable username contains a true value and the value of the variable password is a member of details . 您正在询问变量username包含真实值, 并且变量password的值是否是details的成员 Since details is a dictionary, you are asking if the value is a key in the dictionary. 由于details是字典,因此您要询问值是否是字典中的键。

I suspect you want to test if the current username and password stored in the shelve dictionary are equal to the values entered by the user. 我怀疑您想测试存储在搁置字典中的当前用户名和密码是否等于用户输入的值。 In that case test against the values: 在这种情况下,请针对以下值进行测试:

if details['Name'] == username and details['Password'] == password:
    # Username and password are correct, user is logged in.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM