Python 当用户从字典中输入密钥或值时,仅保存密钥一次
因此,当用户在python中输入密钥本身或密钥值时,我尝试从字典中接收密钥Python 当用户从字典中输入密钥或值时,仅保存密钥一次,python,loops,dictionary,Python,Loops,Dictionary,因此,当用户在python中输入密钥本身或密钥值时,我尝试从字典中接收密钥 dict= {'1':'one','2':'two','3':'three','4':'four','5':'five'} userinput = input('Please choose from the dictionary : ') for key, values in dict.items(): if userinput == key or values: print (key)
dict= {'1':'one','2':'two','3':'three','4':'four','5':'five'}
userinput = input('Please choose from the dictionary : ')
for key, values in dict.items():
if userinput == key or values:
print (key)
所以我在想当用户输入'two'时是'2'还是当用户输入'5'时是'5'
但是我得到的是密钥本身不幸的是,如果x==y或z,我们无法比较像您所示的变量。 试试这个
dict= {'1':'one','2':'two','3':'three','4':'four','5':'five'}
userinput = input('Please choose from the dictionary : ')
for key, values in dict.items():
if userinput == key or userinput == values:
print (key)
print(dict[key])-另外,不要将您的dict称为“dict”,这是一种内部python类型。另外,反转字典,因为这正是您实际要做的。