How to break out of multiple loops?

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

    You can define a variable( for example break_statement ), then change it to a different value when two-break condition occurs and use it in if statement to break from second loop also.

    while True:
        break_statement=0
        while True:
            ok = raw_input("Is this ok? (y/n)")
            if ok == "n" or ok == "N": 
                break
            if ok == "y" or ok == "Y": 
                break_statement=1
                break
        if break_statement==1:
            break
    

提交回复
热议问题