I’m trying to create a dict to not use lots of if statements, but for some reason, I cannot seem to make it work the way I want. I’m trying to pull out only the keys from the dict when the correspond to the inputted day.
Thanks in advance.
edit: expected input/output
Input (Day of Week) | Output (Corresponding key) |
---|---|
‘Monday’ | 12 |
‘Tuesday’ | 12 |
‘Friday’ | 12 |
‘Wednesday’ | 14 |
‘Thursday’ | 14 |
‘Saturday’ | 16 |
‘Sunday’ | 16 |
day = str(input())
day_price_dict = {12: ['Monday', 'Tuesday', 'Friday'], 14: ['Wednesday', 'Thursday'], 16: ['Saturday', 'Sunday']}
if day in day_price_dict:
print(day_price_dict[day])
>Solution :
Following should do what you want:
# example: day = 'Tuesday'
day = str(input())
day_price_dict = {12: ['Monday', 'Tuesday', 'Friday'], 14: ['Wednesday', 'Thursday'], 16: ['Saturday', 'Sunday']}
# iterate through dict keys (12, 14, 16)
for key in day_price_dict:
# if input is in the value list, print the key
if day in day_price_dict[key]:
# print 12
print(key)