How to break out of multiple loops?

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

    Similar like the one before, but more compact. (Booleans are just numbers)

    breaker = False #our mighty loop exiter!
    while True:
        while True:
            ok = get_input("Is this ok? (y/n)")
            breaker+= (ok.lower() == "y")
            break
    
        if breaker: # the interesting part!
            break   # <--- !
    

提交回复
热议问题