How to break out of a double while loop in python?

Python newbie here. How can I break out of the second while loop if the user selects "Q" for "Quit?" If I press "m", it will go to the main menu, and I can exit 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

Application 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: ")
+3
source share
6 answers

You have almost none; you just need to swap these two lines.

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

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

You exit the nested loop before installing loop. :)

+5
source

Edit

break
loop = 0

to

loop = 0
break

in your elif blocks.

+2
source

.

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
+2

, loop1 loop2.

m , , q .

, , , "m". , .

+1
source

Rename your top loop to something like mainloop and set mainloop = 0 when getting q.

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
+1
source

You can put this in 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 understand when you want to break the inner loop and when you want to exit both loops, but that will give you this idea.

0
source

Source: https://habr.com/ru/post/1770546/


All Articles