How to break out of multiple loops?

后端 未结 30 3267
情书的邮戳
情书的邮戳 2020-11-21 05:48

Given the following code (that doesn\'t work):

while True:
    #snip: print out current state
    while True:
        ok = get_input(\"Is this ok? (y/n)\")
          


        
30条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 06:24

    This isn't the prettiest way to do it, but in my opinion, it's the best way.

    def loop():
        while True:
        #snip: print out current state
            while True:
                ok = get_input("Is this ok? (y/n)")
                if ok == "y" or ok == "Y": return
                if ok == "n" or ok == "N": break
            #do more processing with menus and stuff
    

    I'm pretty sure you could work out something using recursion here as well, but I dunno if that's a good option for you.

提交回复
热议问题