-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfood_question.py
More file actions
executable file
·31 lines (26 loc) · 956 Bytes
/
food_question.py
File metadata and controls
executable file
·31 lines (26 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
# Dictionary to keep track of food likes
counter = {}
# Open up the favorite foods log and count frequencies using the dictionary
with open("favorite_foods.log", "r") as f:
for line in f:
item = line.strip()
if item not in counter:
counter[item] = 1
else:
counter[item] += 1
# Print out all the available liked foods
print("Select your favorite food below:")
for item in counter:
print(item)
# Ask which food is the user's favorite
answer = input("Which of the foods above is your favorite? ")
answer = answer.lower()
# Print out how many others like the user's favorite food, otherwise say it can't be found
try:
print("{} of your friends like {} as well!".format(counter[answer], answer))
exit(0)
except KeyError:
print("Hmm we couldn't find anyone who also likes \"{}\".".format(answer))
print("Did you enter one of the foods listed above?")
exit(0)