如何在python中突破double while循环?

时间:2010-10-20 14:25:09

标签: python

新手蟒蛇在这里。如果用户选择“Q”进行“退出?”,如何突破第二个while循环? 如果我点击“m”,它会进入主菜单,然后我就可以退出“Q”键。

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

应用菜单方法:

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

6 个答案:

答案 0 :(得分:5)

你几乎拥有它;你只需要交换这两行。

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

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

在设置loop之前,您将脱离嵌套循环。 :)

答案 1 :(得分:2)

更改

break
loop = 0

loop = 0
break

在您的elif区块中。

答案 2 :(得分:2)

使用例外。

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

答案 3 :(得分:1)

为两个循环使用两个不同的变量,例如loop1loop2

当您第一次在内循环中按m时,您只需在外面打开,然后您可以单独处理q。

顺便说一句,你不需要内部变量来保持循环,只需要一个无限循环,直到按下'm'键。然后你在保持第一个的同时从内循环中突破。

答案 4 :(得分:1)

将你的上层循环重命名为mainloop,并在收到q时设置mainloop = 0.

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

答案 5 :(得分:0)

你可以把它放到一个函数中并返回:

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

不可否认,当你想要打破内循环以及何时想要退出两个循环时,我不太明白,但这会给你一个想法。