jeudi 2 septembre 2021

Go back to if-statement in while loop if statements are NESTED

while True:
    if answer in '1':
        
                                #VANILLA MENU
        extra_bag = 0
        extra_meal = 0
        ticket_value += 500
        bags_registered = 0
        meals_registered = 0

        print("Currently you have:")
        print("\n")
        print(bags_registered , "bag(s) registered")
        print(meals_registered , "meal(s) registered")
        print("\n")

        print("Here are you options")
        print("(1) Add bag (max 1)")
        print("(2) Add meal (max 1)")
        print("(3) Remove bag")
        print("(4) Remove meal")
        print("(5) Finalize ticket")

        answer = input("Your choice >> ")

        while True:
            if answer in '1':
                extra_bag += 200
                extra_meal = 0
                bags_registered = 1
                meals_registered = 0

                print("Currently you have:")
                print("\n")
                print(bags_registered , "bag(s) registered")
                print(meals_registered , "meal(s) registered")
                print("\n")
                print("(1) Add bag (max 1)")
                print("(2) Add meal (max 1)")
                print("(3) Remove bag")
                print("(4) Remove meal")
                print("(5) Finalize ticket")

                answer = input("Your choice >> ")

                if answer in '1':
                    print("\nYou have already added a bag\n")

                elif answer in '2':
                    extra_bag = 200
                    extra_meal = 150
                    bags_registered = 1
                    meals_registered = 1

                    print("Currently you have:")
                    print("\n")
                    print(bags_registered , "bag(s) registered")
                    print(meals_registered , "meal(s) registered")
                    print("\n")
                    print("(1) Add bag (max 1)")
                    print("(2) Add meal (max 1)")
                    print("(3) Remove bag")
                    print("(4) Remove meal")
                    print("(5) Finalize ticket")

                    answer = input("Your choice >> ")          

                    if answer in '1':
                        print("You have already added a bag")
                    elif answer in '2':
                        print("You have already added a meal")
                    elif answer in '4':
                        print("\n")
                        extra_meal = 0
                        meals_registered = 0

I need the last elif statement to allow the user to return to the first if statement, since they decided to remove their bag. Is there a simpler way of doing this? I am very new to python and this is doing my head in. I thought about making a function for the "vanilla menu" but I'm not sure that would work.

Aucun commentaire:

Enregistrer un commentaire