1. <tfoot id='YnwIC'></tfoot>

  2. <legend id='YnwIC'><style id='YnwIC'><dir id='YnwIC'><q id='YnwIC'></q></dir></style></legend>
  3. <i id='YnwIC'><tr id='YnwIC'><dt id='YnwIC'><q id='YnwIC'><span id='YnwIC'><b id='YnwIC'><form id='YnwIC'><ins id='YnwIC'></ins><ul id='YnwIC'></ul><sub id='YnwIC'></sub></form><legend id='YnwIC'></legend><bdo id='YnwIC'><pre id='YnwIC'><center id='YnwIC'></center></pre></bdo></b><th id='YnwIC'></th></span></q></dt></tr></i><div id='YnwIC'><tfoot id='YnwIC'></tfoot><dl id='YnwIC'><fieldset id='YnwIC'></fieldset></dl></div>
  4. <small id='YnwIC'></small><noframes id='YnwIC'>

    • <bdo id='YnwIC'></bdo><ul id='YnwIC'></ul>


      How to break out of multiple loops?(如何打破多个循环?)
          <bdo id='hcdBO'></bdo><ul id='hcdBO'></ul>
        • <i id='hcdBO'><tr id='hcdBO'><dt id='hcdBO'><q id='hcdBO'><span id='hcdBO'><b id='hcdBO'><form id='hcdBO'><ins id='hcdBO'></ins><ul id='hcdBO'></ul><sub id='hcdBO'></sub></form><legend id='hcdBO'></legend><bdo id='hcdBO'><pre id='hcdBO'><center id='hcdBO'></center></pre></bdo></b><th id='hcdBO'></th></span></q></dt></tr></i><div id='hcdBO'><tfoot id='hcdBO'></tfoot><dl id='hcdBO'><fieldset id='hcdBO'></fieldset></dl></div>

                  <tbody id='hcdBO'></tbody>
              1. <small id='hcdBO'></small><noframes id='hcdBO'>

                <tfoot id='hcdBO'></tfoot>
                <legend id='hcdBO'><style id='hcdBO'><dir id='hcdBO'><q id='hcdBO'></q></dir></style></legend>



                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)")
                        if ok.lower() == "y": break 2 #this doesn't work :(
                        if ok.lower() == "n": break
                    #do more processing with menus and stuff


                Is there a way to make this work? Or do I have do one check to break out of the input loop, then another, more limited, check in the outside loop to break out all together if the user is satisfied?



                Here's another approach that is short. The disadvantage is that you can only break the outer loop, but sometimes it's exactly what you want.

                for a in xrange(10):
                    for b in xrange(20):
                        if something(a, b):
                            # Break the inner loop...
                        # Continue if the inner loop wasn't broken.
                    # Inner loop was broken, break the outer.

                这使用了 for/else 结构,解释如下:为什么python在for和while循环之后使用'else'?

                This uses the for / else construct explained at: Why does python use 'else' after for and while loops?


                Key insight: It only seems as if the outer loop always breaks. But if the inner loop doesn't break, the outer loop won't either.

                continue 语句是这里的魔法.它在 for-else 子句中.根据定义,如果没有内部中断,就会发生这种情况.在那种情况下,continue 巧妙地绕过了外部中断.

                The continue statement is the magic here. It's in the for-else clause. By definition that happens if there's no inner break. In that situation continue neatly circumvents the outer break.




                Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                How to put the legend out of the plot(如何将传说从情节中剔除)
                Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                Difference between modes a, a+, w, w+, and r+ in built-in open function?(内置 open 函数中模式 a、a+、w、w+ 和 r+ 的区别?)
                    <tbody id='BfQTr'></tbody>

                  1. <small id='BfQTr'></small><noframes id='BfQTr'>

                  2. <legend id='BfQTr'><style id='BfQTr'><dir id='BfQTr'><q id='BfQTr'></q></dir></style></legend>

                        <bdo id='BfQTr'></bdo><ul id='BfQTr'></ul>
                        <i id='BfQTr'><tr id='BfQTr'><dt id='BfQTr'><q id='BfQTr'><span id='BfQTr'><b id='BfQTr'><form id='BfQTr'><ins id='BfQTr'></ins><ul id='BfQTr'></ul><sub id='BfQTr'></sub></form><legend id='BfQTr'></legend><bdo id='BfQTr'><pre id='BfQTr'><center id='BfQTr'></center></pre></bdo></b><th id='BfQTr'></th></span></q></dt></tr></i><div id='BfQTr'><tfoot id='BfQTr'></tfoot><dl id='BfQTr'><fieldset id='BfQTr'></fieldset></dl></div>
                          <tfoot id='BfQTr'></tfoot>