How to break out of multiple loops?

后端 未结 30 3320
情书的邮戳
情书的邮戳 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:09

    
    keeplooping=True
    while keeplooping:
        #Do Stuff
        while keeplooping:
              #do some other stuff
              if finisheddoingstuff(): keeplooping=False
    

    or something like that. You could set a variable in the inner loop, and check it in the outer loop immediately after the inner loop exits, breaking if appropriate. I kinda like the GOTO method, provided you don't mind using an April Fool's joke module - its not Pythonic, but it does make sense.

提交回复
热议问题