How to break out of double while loop in python? – Education Career Blog

Newbie python here. How can I break out of the second while loop if a user selects “Q” for “Quit?”
If I hit “m,” it goes to the main menu and there I can quit hitting the “Q” key.

while loop == 1:
    choice = main_menu()

    if choice == "1":
        os.system("clear")

        while loop == 1:
            choice = app_menu()

            if choice == "1":
                source = '%s/%s/external' % (app_help_path,app_version_10)
                target = '%s/%s' % (target_app_help_path,app_version_10)

            elif choice == "2":
                source = '%s/%s/external' % (app_help_path,app_version_8)
                target = '%s/%s' % (target_app_help_path,app_version_8)
            elif choice.lower() == "m":
                break
                loop = 0
            elif choice.lower() == "q":
                break
                loop = 0
            sendfiles(source, target)

    # Internal files

    elif choice == "q":
        loop = 0

App menu method:

def app_menu()
    print "Select APP version"
    print "-------------------"
    print "1) 11"
    print "2) 10"
    print "3) 8"
    print "m) Main Menu"
    print "q) Quit"
    print
    return raw_input("Select an option: ")

,

You nearly have it; you just need to swap these two lines.

elif choice.lower() == "m":
    break
    loop = 0

elif choice.lower() == "m":
     loop = 0
     break

You break out of the nested loop before setting loop. 🙂

,

Change

break
loop = 0

to

loop = 0
break

in your elif blocks.

,

Use an exception.

class Quit( Exception ): pass

running= True
while running:
    choice = main_menu()

    if choice == "1":
        os.system("clear")

        try:
            while True:
                choice = app_menu()

                if choice == "1":

                elif choice == "2":

                elif choice.lower() == "m":
                    break
                    # No statement after break is ever executed.
                elif choice.lower() == "q":
                    raise Quit
                sendfiles(source, target)

         except Quit:
             running= False

    elif choice == "q":
        running= False

,

Use two distinct variables for both loops, eg loop1 and loop2.

When you first press m in the inner loop you just break outside, and then you can handle q separately.

By the way you shouldn’t need the inner variable to keep looping, just go with an infinite loop until key ‘m’ is pressed. Then you break out from inner loop while keeping first one.

,

Rename your top loop to something like mainloop, and set mainloop = 0 when q is received.

while mainloop == 1:
    choice = main_menu()
    if choice == "1":
        os.system("clear")

        while loop == 1:
            choice = app_menu()

            if choice == "1":
                source = '%s/%s/external' % (app_help_path,app_version_10)
                target = '%s/%s' % (target_app_help_path,app_version_10)

            elif choice == "2":
                source = '%s/%s/external' % (app_help_path,app_version_8)
                target = '%s/%s' % (target_app_help_path,app_version_8)
            elif choice.lower() == "m":
                loop = 0
                break
            elif choice.lower() == "q":
                mainloop = 0break
                break
            sendfiles(source, target)

    # Internal files

    elif choice == "q":
        mainloop = 0

,

You could put this into a function and return:

import os.path
def do_whatever():
    while True:
        choice = main_menu()

        if choice == "1":
            os.system("clear")

        while True:
            choice = app_menu()

            if choice in ("1", "2"):
                app_version = app_version_10 if choice == "1" else app_version_8
                source = os.path.join(app_help_path, app_version, "external")
                target = os.path.join(target_app_help_path, app_version)
                sendfiles(source, target)
            elif choice.lower() == "m":
                break
            elif choice.lower() == "q":
                return

Admittedly, I don’t quite get when you want to break the inner loop and when you want to quit both loops, but this will give you the idea.

Leave a Comment