tags:

views:

146

answers:

6

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: ")
+1  A: 

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.

Jack
+3  A: 

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. :)

Aphex
ok looks like it works, but I modified it for the "q" elif.
luckytaxi
+2  A: 

Change

break
loop = 0

to

loop = 0
break

in your elif blocks.

Mikael S
+1  A: 

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
Andrew Sledge
`mainloop = 0` and `loop = 0` never get hit in your `elif` blocks, so this wouldn't work
Daniel DiPaolo
You overlooked the same little bug that luckytaxi did; see my answer. :)
Aphex
@Aphex: I wouldn't call it *little*
SilentGhost
@Aphex, true, but that wasn't the question. The question is "How can I break out of the second while loop if a user selects "Q" for "Quit?"" Using two loops solves this.
Andrew Sledge
+1  A: 

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
S.Lott
this doesnt work. Hitting "q" inside the second menu doesn't do a thing.
luckytaxi
A: 

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.

hughdbrown