-1

So I am trying to receive the Key from the dictionary when a user inputs the key itself or the value of the key in 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)

So I was thinking the when the user entered 'two' to be '2' or when user entered '5' to be '5'

But rather I am getting the key it self

Jay Jay
  • 33
  • 8

1 Answers1

0

Sadly, we cannot compare variables like you have shown if x == y or z. Instead try this

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)
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44