如何阻止我的代码在 1 次选择后结束?

问题描述 投票:0回答:1

def main():
    Bananas=0
    Strawberries=0
    Blueberries=0
    Spinach=0
    Avocado=0
    
    ingredients = [Bananas,Strawberries,Blueberries,Spinach,Avocado]
    
    print("Welcome to the Smoothie Recipe Analyzer!\n")
    quantity_input(ingredients)
    print("")

    # Option Selection
    decision = main_menu()
    if (decision == 1):
        view_ingredients(ingredients)

    if (decision == 2):
        update_ingredients(ingredients)

    if (decision == 3):
        make_smoothie(ingredients)

    main_menu()

# Function 1 - Getting the ingredient data from the user
def quantity_input(ingredients):
    ingredients[0]=float(input("How many bananas do you have?\n"))
    while (ingredients[0]<0):
        ingredients[0]=float(input("Invalid amount. How many bananas do you have?\n"))
            
    ingredients[1]=float(input("How many strawberries do you have?\n"))
    while (ingredients[1]<0):
        ingredients[1]=float(input("Invalid amount. How many strawberries do you have?\n"))
            
    ingredients[2]=float(input("How many blueberries do you have?\n"))
    while (ingredients[2]<0):
        ingredients[2]=float(input("Invalid amount. How many blueberries do you have?\n"))
            
    ingredients[3]=float(input("How many cups of spinach do you have?\n"))
    while (ingredients[3]<0):
        ingredients[3]=float(input("Invalid amount. How many cups of spinach do you have?\n"))
            
    ingredients[4]=float(input("How many avocados do you have?\n"))
    while (ingredients[4]<0):
        ingredients[4]=float(input("Invalid amount. How many avocado do you have?\n"))

# Function 2 - Building the main menu
def main_menu():
    print("")
    print("What would you like to do?\n")
    
    options = ["1 - View Ingredients","2 - Update Ingredients","3 - Make a Smoothie","4 - Quit\n"]
    for i in options:
        print(i)

    choice=int(input(""))
    return choice

# Function 3 - Viewing the ingredients
def view_ingredients(ingredients):
    print("Bananas:",ingredients[0],"")
    print("Strawberries:",ingredients[1],"")
    print("Blueberries:",ingredients[2],"")
    print("Spinach(Cups):",ingredients[3],"")
    print("Avocado:",ingredients[4],"")

# Function 4 - Updating the ingredients
def update_ingredients(ingredients):
    ingredients[0]=float(input("How many bananas do you have?\n"))
    while (ingredients[0]<0):
        ingredients[0]=float(input("Invalid amount. How many bananas do you have?\n"))
            
    ingredients[1]=float(input("How many strawberries do you have?\n"))
    while (ingredients[1]<0):
        ingredients[1]=float(input("Invalid amount. How many strawberries do you have?\n"))
            
    ingredients[2]=float(input("How many blueberries do you have?\n"))
    while (ingredients[2]<0):
        ingredients[2]=float(input("Invalid amount. How many blueberries do you have?\n"))
            
    ingredients[3]=float(input("How many cups of spinach do you have?\n"))
    while (ingredients[3]<0):
        ingredients[3]=float(input("Invalid amount. How many cups of spinach do you have?\n"))
            
    ingredients[4]=float(input("How many avocados do you have?\n"))
    while (ingredients[4]<0):
        ingredients[4]=float(input("Invalid amount. How many avocado do you have?\n"))

# Function 5 - Make a Smoothie
def make_smoothie(ingredients):    
    # User input on which fruite to use
    use_bananas=input("Will you use bananas?\n")
    while (use_bananas != "yes" and use_bananas != "no"):
        use_bananas=input("Invalid Response. Will you use bananas?\n")
    if (use_bananas == "yes"):
        ingredients[0] -= 1

    use_strawberries=input("Will you use strawberries?\n")
    while (use_strawberries != "yes" and use_strawberries != "no"):
        use_strawberries=input("Invalid Response. Will you use strawberries?\n")
    if (use_strawberries == "yes"):
        ingredients[1] -= 5
        
    use_blueberries=input("Will you use blueberries?\n")
    while (use_blueberries != "yes" and use_blueberries != "no"):
        use_blueberries=input("Invalid Response. Will you use blueberries?\n")
    if (use_blueberries == "yes"):
        ingredients[2] -= 12
        
    use_spinach=input("Will you use spinach?\n")
    while (use_spinach != "yes" and use_spinach != "no"):
        use_spinach=input("Invalid Response. Will you use spinach?\n")
    if (use_spinach == "yes"):
        ingredients[3] -= 1
        
    use_avocado=input("Will you use avocado?\n")
    while (use_avocado != "yes" and use_avocado != "no"):
        use_avocado=input("Invalid Response. Will you use avocado?\n")
    if (use_avocado == "yes"):
        ingredients[4] -= 0.5
        
    # Check fruite to make sure the smoothie can be made
    if (use_bananas == "yes" and ingredients[0]<1):
        print("Sorry, this recipe cannot be completed.\n")

    if (use_strawberries == "yes" and ingredients[1]<5):
        print("Sorry, this recipe cannot be completed.\n")

    if (use_blueberries == "yes" and ingredients[2]<12):
        print("Sorry, this recipe cannot be completed.\n")

    if (use_spinach == "yes" and ingredients[3]<1):
        print("Sorry, this recipe cannot be completed.\n")
        
    if (use_avocado == "yes" and ingredients[4]<0.5):
        print("Sorry, this recipe cannot be completed.\n")

    # Drink score determination
    drink_score = 1
    if (use_spinach == "yes" and use_avocado == "yes"):
        drink_score = 0
        
    # Health score determination
    health_score = 1
    if (use_spinach == "no" and use_avocado == "no"):
        health_score = 0

    if (use_spinach == "yes" and use_avocado == "yes"):
        health_score = 2

    # Drink score messages
    if (drink_score == 1):
        print("Your recipe scored a Drink Score of 1. It will be delicious!")

    if (drink_score == 0):
        print("Your recipe scored a Drink Score of 0. Yuck!")

    # Health score messages
    if (health_score == 2):
        print("Your recipe scored a Health Score of 2. It will be super healthy!")

    if (health_score == 1):
        print("Your recipe scored a Health Score of 1. It is good to go!")

    if (health_score == 0):
        print("Your recipe scored a Health Score of 0. It probably tastes good though.")
main()

我必须为学校制作一个冰沙程序,它要求我构建一个主菜单功能,为用户提供一个选项列表,然后返回他们对要使用的主功能的选择。当我运行它时,它工作正常,但是当主菜单再次出现并且我输入我的选择时,代码就结束了。我认为问题主要在于 def main 和 def main_menu 函数enter image description here。非常感谢任何帮助!

我已经尝试了我所知道的一切。

python list function
1个回答
0
投票

您应该使用 while 循环,就像您在代码的其他部分中所做的那样

while True:
    # Option Selection
    try:
        # enter non number to exit the loop
        decision = main_menu()
        if decision == 1:
            ...
    except:
        break
© www.soinside.com 2019 - 2024. All rights reserved.