'How to print a single line from a dictonary in python 3
Beginner here writing a code in IDLE that goes like this. I need it to prompt the user to enter a stall number. Then tell the user the animal, food type, and feeding time associated with that stall. This is the code I have to accompany this.
stalls = {101: 'Zebras',
102: 'Elephants',
103: 'Geese',
104: 'Sheep',
105: 'Turtle',
106: 'Cheetah',
107: 'Giraffe',
108: 'Capybara',
109: 'Monkeys',
110: 'Turkey',
111: 'Guinea Pigs',
112: 'Possum',
113: 'Goats',
114: 'Lions',
115: 'Tigers'}
food_types = {101: 'Grass',
102: 'Vegetables',
103: 'Worms',
104: 'Grass',
105: 'Vegetables',
106: 'Meat',
107: 'Vegetables',
108: 'Vegetables',
109: 'Fruit',
110: 'Worms',
111: 'Fruit',
112: 'Worms',
113: 'Grass',
114: 'Meat',
115: 'Meat'}
feeding_times = {101: '0730',
102: '0900',
103: '0845',
104: '0930',
105: '0745',
106: '0805',
107: '0800',
108: '0808',
109: '0953',
110: '0738',
111: '0747',
112: '2000',
113: '1005',
114: '0923',
115: '0843'}
stall_choice = input (int("Which animal are you looking for? Please enter stall number. "))
If I input 111, I am expecting an output of something like "stall number 111 eats Fruit at 0747" How do I print a dictionary by specific line? I am aware there are other ways of creating a program like this but I am getting some practice with the use of dictionaries. Thank you!
Solution 1:[1]
Use dictionary[key]. Also, it's int(input()), not input(int()). Take a look below:
# Define dictionaries
stall_choice = int(input('...'))
print(f"stall number {stall_choice} (which is {stalls[stall_choice]}), eats {food_types[stall_choice]} at time {feeding_times[stall_choice]}")
Here, I've used f-strings, but you could also use .format().
Output:
stall number 111 (which is Guinea Pigs), eats Fruit at time 0747
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Codeman |
