因此,当用户在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)
所以我想当用户输入'two'时为'2',或者当用户输入'%5'时为'5'
但我得到了密钥,它自己
遗憾的是,我们无法像您展示的if x == y or 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)