Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading