How to break out of multiple loops?

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

    And why not to keep looping if two conditions are true? I think this is a more pythonic way:

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

    Isn't it?

    All the best.

提交回复
热议问题