代码之家  ›  专栏  ›  技术社区  ›  sdot257

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

  •  3
  • sdot257  · 技术社区  · 14 年前

    新来的蟒蛇。如果用户选择“Q”作为“Quit”,我怎样才能跳出第二个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 回复  |  直到 14 年前
        1
  •  5
  •   Aphex Bodhi Hu    14 年前

    你差一点就拿到了,你只要把这两条线换一下就行了。

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

    在设置 loop

        2
  •  2
  •   Mikael S    14 年前

    改变

    break
    loop = 0
    

    loop = 0
    break
    

        3
  •  2
  •   S.Lott    14 年前

    使用异常。

    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
    
        4
  •  1
  •   Jack    14 年前

    对两个循环使用两个不同的变量,例如 loop1 loop2

    当你第一次在内环中按m时,你只需在外环断开,然后你可以分别处理q。

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

        5
  •  1
  •   Andrew Sledge    14 年前

    将top循环重命名为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
    
        6
  •  0
  •   hughdbrown    14 年前

    您可以将其放入函数并返回:

    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
    

    诚然,我不太明白什么时候你想打破内环,什么时候你想退出两个环,但这会给你一个想法。