How to break out of multiple loops?

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

    Try using an infinite generator.

    from itertools import repeat
    inputs = (get_input("Is this ok? (y/n)") for _ in repeat(None))
    response = (i.lower()=="y" for i in inputs if i.lower() in ("y", "n"))
    
    while True:
        #snip: print out current state
        if next(response):
            break
        #do more processing with menus and stuff
    

提交回复
热议问题