Hi there. I’m messing around with Python dictionaries. This code takes you into a menu with available foods and lets you consume them for HP. It then updates your health and pops off the food for the dict.
I want to add a “none” option to the food menu. You can see the elif statement is #commented out. Using a break only took me to the main().
Any ideas for adding that and any comments for improving the code?
import time
import sys
foods = {"blueberries": 20, "apple": 30, "banana": 15, "pizza": 5, "broccoli": 60,
"milkshake": 10, "soda": 5, "turkey leg": 50}
health = 0
print("FOOD MENU")
def eat_food():
global health
global foods
print("You have... ")
while True:
for food, health_points in foods.items():
print("{0}: {1} HP".format(food, health_points))
print("Type 'none' to exit food menu.")
user_input = input("Which food would you like to eat?: ")
if user_input in foods.keys():
health += foods.get(user_input)
foods.pop(user_input)
print("Your current health level is now {0}.".format(health))
#elif user_input in ("None", "none"):
else:
print("You don't have that item. Check for typos.")
return health
def main():
eat_food()
time.sleep(0.5)
while True:
user_input_2 = input("Would you like to eat something else?: ")
if user_input_2 in ("Yes", "yes"):
eat_food()
elif user_input_2 in ("No", "no"):
print("Exiting food menu.")
break
else:
user_unsure = input("Sorry, was that a yes or no?: ")
if user_unsure in ("Yes", "yes"):
eat_food()
elif user_unsure in ("No", "no"):
print("Exiting food menu.")
break
if __name__ == "__main__":
main()