Search code examples
pythonpython-3.xwhile-loopbreak

Python: Breaking out of inner loop to a specific condition in the outer while loop


This program runs fine but it would be better if I could break out of the inner loop to a specific condition in the outer while loop. Basically, I don't want to run the whole loop again because it is redundant to display the main menu again. If the answer is "no" it should just go to the the 'review and exit' option. How can I go from line 52 (inner loop) to line 84 (condition in outer loop)?

def menu():   
    print('Main Menu')
    print('1) Chair')
    print('2) Table')
    print('3) Review and Exit')

def menu2():
    print('')
    print('1) Yes')
    print('2) No')

item1_count = 0
item2_count = 0
chair_count = 0
table_count = 0
loop = 1

while loop == 1:
    menu()
    while True:
        try:
            q = int(input('Choose an item: '))
        except ValueError:
            print('Choose 1-3.')
            continue
        else:
            break

    if q == 1:
        item1_count = item1_count + q
        while True:
            try:
                q2 = int(input('How many chairs? '))
            except ValueError:
                print('Type a number.')
                continue
            else:
                break
        chair_count = chair_count + q2
        menu2()
        while True:
            try:
                q3 = int(input('Would you like anything else? '))
            except ValueError:
                print('Choose 1 or 2.')
                continue
            if q3 == 1:
                print('yes')
                break
            elif q3 == 2:
                print('no.')
                # i want to go to line 83 here if i choose no.. how?
                # 'break' takes me back to the main loop (line 19)
                break

    elif q == 2:
        item2_count = item2_count + q
        while True:
            try:
                q4 = int(input('How many tables? '))
            except ValueError:
                print('Type a number.')
                continue
            else:
                break
        table_count = table_count + q4
        menu2()
        while True:
            try:
                q3 = int(input('Would you like anything else? '))
            except ValueError:
                print('Choose 1 or 2.')
                continue
            if q3 == 1:
                print('yes')
                break
            elif q3 == 2:
                print('no.')
                # i want to go to line 83 here if i choose no.. how?
                # 'break' takes me back to the main loop (line 19)
                break
    
    elif q == 3:
        print('You ordered', chair_count, 'chair(s) and', table_count, 'table(s).')
        break
    else:
        print('Choose 1-3.')
        continue

print(item1_count)
print(round(item2_count / 2))
print(chair_count)
print(table_count)

Solution

  • I am not sure about breaking the inner loop based on the outer loop condition unless we check the condition in every iteration of the inner loop.

    But I hope the below modification will result in what you want:

    def menu():   
        print('Main Menu')
        print('1) Chair')
        print('2) Table')
        print('3) Review and Exit')
    
    def menu2():
        print('')
        print('1) Yes')
        print('2) No')
    
    item1_count = 0
    item2_count = 0
    chair_count = 0
    table_count = 0
    loop = 1
    stopped=False
    
    while loop == 1:
       
        while stopped==False:
            menu()
            try:
                q = int(input('Choose an item: '))
            except ValueError:
                print('Choose 1-3.')
                continue
            else:
                break
    
        if q == 1:
            item1_count = item1_count + q
            while True:
                try:
                    q2 = int(input('How many chairs? '))
                except ValueError:
                    print('Type a number.')
                    continue
                else:
                    break
            chair_count = chair_count + q2
            menu2()
            while True:
                try:
                    q3 = int(input('Would you like anything else? '))
                except ValueError:
                    print('Choose 1 or 2.')
                    continue
                if q3 == 1:
                    print('yes')
                    break
                elif q3 == 2:
                    print('no.')
                    stopped=True
                    q=3
                    break
                    
    
        elif q == 2:
            item2_count = item2_count + q
            while True:
                try:
                    q4 = int(input('How many tables? '))
                except ValueError:
                    print('Type a number.')
                    continue
                else:
                    break
            table_count = table_count + q4
            menu2()
            while True:
                try:
                    q3 = int(input('Would you like anything else? '))
                except ValueError:
                    print('Choose 1 or 2.')
                    continue
                if q3 == 1:
                    print('yes')
                    break
                elif q3 == 2:
                    print('no.')
                    q=3
                    stopped=True
                    break
                   
        
        elif q == 3:
            print('You ordered', chair_count, 'chair(s) and', table_count, 'table(s).')
            break
        else:
            print('Choose 1-3.')
            continue
    
    print(item1_count)
    print(round(item2_count / 2))
    print(chair_count)
    print(table_count)
    

    Here we defined a variable at the global level named stopped which tells the user wants to stop running the loop and we are setting it to True when user wants to quit. If it is true, we'll stop asking for input. Also, we assigned a value of 3 to variable q to redirect the if statements to print review. Kindly note that we shifted the calling menu() function to inside the while loop.

    But try to design the flow with less number of if else and while statements. It can be done for the above case.

    I hope this helps.