Eat food for HP with Python Dictionaries - Help

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()

Why would it do anything else? break at that point in your eat_food() function terminates your while loop, bringing the execution of your eat_food function to a close.

Depending on whether this is the first call to eat_food, you either proceed into or remain inside of the while loop defined in main

What were you expecting to happen instead? :slight_smile:

Also, the while loop inside of eat_food is kinda pointless… because you only run it once, so not much need for the looping bit, eh? :slight_smile:

Alright so I got rid of the while loop in eat_food(). How would I go about stopping the main() function after someone enters “none” in the eat_food() function? I tried using sys.exit() but then leaves the terminal blank.