How to stop loop until user inputs a keyword to stop?

前端 未结 2 1192
别那么骄傲
别那么骄傲 2021-01-29 13:55

My code right now is in an infinite loop, displaying the menu choices for doughnuts. I want it so that the user selects as many doughnuts as they want until they input \"5\". <

2条回答
  •  执笔经年
    2021-01-29 14:16

    There are a few problems here.

    The first is that the logic to respond to a choice is outside your while loops. That can be fixed by indenting that whole block.

    Second when the user inputs 5, the condition in while choice not in [1,2,3,4]: evaluates to True, so the user is prompted to enter a valid choice again. This can be fixed by removing that inner while loop entirely.

    Finally upon reaching the elif choice == 5 block, the user will not see any of these receipt prints because choice is 5 and therefore not 1, 2, 3, or 4. I think what you mean here is for the count of chocolate, strawberry, vanilla, or honey to be nonzero. Also these should all be if rather than elif blocks since they are independent of each other (a user can get some chocolate and some vanilla).

    With all that in mind here is a refactor:

    print("Welcome to Dino's International Doughnut Shoppe!")
    name = input("Please enter your name to begin: ")
    
    #doughnuts menu
    chocolate = strawberry = vanilla = honey = 0
    done = False
    while not done:
        print("Please enter a valid choice from 1-4.")
        print("Please select a doughnut from the following menu: ")
        print("1. Chocolate-dipped Maple Puff ($3.50 each)")
        print("2. Strawberry Twizzler ($2.25 each)")
        print("3. Vanilla Chai Strudel ($4.05 each)")
        print("4. Honey-drizzled Lemon Dutchie ($1.99)")
        print("5. No more doughnuts.")
        choice = int(input(">"))
    
    
        if choice == 1:
            chocolate = int(input("How many chocolate-dipped Maple Puff(s) would you like to purchase? "))
        elif choice == 2:
            strawberry = int(input("How many Strawberry Twizzler(s) would you like to purchase? "))
        elif choice == 3:
            vanilla = int(input("How many Vanilla Chai Strudel(s) would you like to purchase? "))
        elif choice == 4:
            honey = int(input("How many Honey-drizzled Lemon Dutchie(s) would you like to purchase? "))
        elif choice == 5:
            done = True
            print(f"{name}, Here is your receipt: ")
    
            if chocolate > 1:
                print("==========================================")
                print(f"{chocolate} Chocolate Dipped Maple Puffs")
                print("==========================================")
                print(f"Total Cost: ${chocolate*3.50:.2f}")
            if strawberry > 1:
                print("==========================================")
                print(f"{strawberry} Strawberry Twizzlers")
                print("==========================================")
                print(f"Total Cost: ${strawberry*2.25:.2f}")
            if vanilla > 1:
                print("==========================================")
                print(f"{vanilla} Vanilla Chai Strudels")
                print("==========================================")
                print(f"Total Cost: ${vanilla*4.05:.2f}")
            if honey > 1:
                print("==========================================")
                print(f"{honey} Honey-drizzled Lemon Dutchies")
                print("==========================================")
                print(f"Total Cost: ${honey*1.99:.2f}")
    
        print("Thank you for shopping at Dino's International Doughnut Shoppe! Please come again!")
    

提交回复
热议问题