Maison >développement back-end >Tutoriel Python >如何在Python中使用break跳出多层循环?
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span> <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">20</span><span class="p">):</span> <span class="k">if</span> <span class="n">something</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="c"># Break the inner loop...</span> <span class="k">break</span> <span class="k">else</span><span class="p">:</span> <span class="c"># Continue if the inner loop wasn't broken.</span> <span class="k">continue</span> <span class="c"># Inner loop was broken, break the outer.</span> <span class="k">break</span>