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

Time:08-28

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)

CodePudding user response:

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.

CodePudding user response:

Here is my solution:

Changes:

line 19: Initialized q3 in the beginning
line 23: Added if statement
line 32: Added elif statement

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
q3 = 0  # initialize q3 from the start because it can cause NameError on line 23 otherwise

while loop == 1:
    while True:
        if q3 != 2:  # create this condition, so if the user chose number 2 in the last question the code below will not execute
            menu()
            try:
                q = int(input('Choose an item: '))
            except ValueError:
                print('Choose 1-3.')
                continue
            else:
                break
        elif q3 == 2:  # and if the user chose 2 in the last question, we will set var q to 3 and break from the loop
            q = 3
            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)

Recommendation

Honestly, this program sucks, it's not readable and hard to fix bugs. I'd suggest you to try to split this program in functions, this code will be more readable and more professional.

You can read this article to have a better understanding.

  • Related