Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当用户从字典中输入密钥或值时,仅保存密钥一次_Python_Loops_Dictionary - Fatal编程技术网

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)

因此,当用户在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'


但是我得到的是密钥本身

不幸的是,如果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类型。另外,反转字典,因为这正是您实际要做的。