peeenut
peeenut

Reputation: 13

Python: Having issues displaying values from a dictionary

I'm having issues displaying the keys from within a dictionary based on user input. Whenever I run the code without the hour -=1, it only displays 'doce' when I'm trying to display whatever number the user inputs. If I allow the hour -= 1 in, it displays 'once'. I've even tried turning the input into an integer but I still get the same issues. Thank you for help in advance.

conversion = {1: 'uno', 2: 'dos', 3:'tres', 4:'cuatro', 5: 'cinco', 6: 'seis', 7:'siete', 8:'ocho', 9:'nueve', 10: 'dies', 11: 'once', 12:'doce' }
for hour in conversion:
    hour -= 1
    new_hour = conversion.get(hour)
print (new_hour) 

Upvotes: 0

Views: 61

Answers (1)

Steve Deng Zishi
Steve Deng Zishi

Reputation: 128

If you want to iterate through the whole dictionary then write the print statement inside the for loop will do.

If you want to display value based on user input key:

key = int(input("please enter a key: "))
value = conversion[key]
print(value)

It would be more helpful to show us the full code and explains exactly what would like to do here.

Upvotes: 1

Related Questions