{"id":1032582,"date":"2024-12-31T11:34:47","date_gmt":"2024-12-31T03:34:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032582.html"},"modified":"2024-12-31T11:34:49","modified_gmt":"2024-12-31T03:34:49","slug":"python%e5%a6%82%e4%bd%95%e8%b7%b3%e5%87%ba%e4%b8%a4%e4%b8%aawhile%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032582.html","title":{"rendered":"python\u5982\u4f55\u8df3\u51fa\u4e24\u4e2awhile\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/12088f11-401a-45b5-b174-d40c427e2615.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8df3\u51fa\u4e24\u4e2awhile\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8df3\u51fa\u5d4c\u5957\u7684\u4e24\u4e2a while \u5faa\u73af\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 break\u3001\u4f7f\u7528\u51fd\u6570\u548c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 break \u662f\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 break \u662f\u4e00\u79cd\u63a7\u5236\u6d41\u7a0b\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6807\u8bb0\u53d8\u91cf\uff0c\u7136\u540e\u5728\u5185\u5c42\u5faa\u73af\u4e2d\u8bbe\u7f6e\u8fd9\u4e2a\u6807\u8bb0\u53d8\u91cf\uff0c\u5e76\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u6839\u636e\u8fd9\u4e2a\u6807\u8bb0\u53d8\u91cf\u51b3\u5b9a\u662f\u5426\u8df3\u51fa\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 break<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6807\u8bb0\u53d8\u91cf <code>flag<\/code>\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a <code>True<\/code> \u6216 <code>False<\/code> \u6765\u51b3\u5b9a\u662f\u5426\u8df3\u51fa\u5916\u5c42\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">flag = False<\/p>\n<p>while True:  # \u5916\u5c42\u5faa\u73af<\/p>\n<p>    while True:  # \u5185\u5c42\u5faa\u73af<\/p>\n<p>        print(&quot;Inside inner loop&quot;)<\/p>\n<p>        # \u67d0\u4e9b\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u8bbe\u7f6e\u6807\u8bb0\u53d8\u91cf\u5e76\u8df3\u51fa\u5185\u5c42\u5faa\u73af<\/p>\n<p>        if condition:<\/p>\n<p>            flag = True<\/p>\n<p>            break<\/p>\n<p>    # \u6839\u636e\u6807\u8bb0\u53d8\u91cf\u51b3\u5b9a\u662f\u5426\u8df3\u51fa\u5916\u5c42\u5faa\u73af<\/p>\n<p>    if flag:<\/p>\n<p>        break<\/p>\n<p>    print(&quot;Inside outer loop&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53 <code>condition<\/code> \u6ee1\u8db3\u65f6\uff0c\u8bbe\u7f6e\u6807\u8bb0\u53d8\u91cf <code>flag<\/code> \u4e3a <code>True<\/code> \u5e76\u8df3\u51fa\u5185\u5c42\u5faa\u73af\u3002\u7136\u540e\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u68c0\u67e5\u6807\u8bb0\u53d8\u91cf <code>flag<\/code>\uff0c\u5982\u679c\u4e3a <code>True<\/code>\uff0c\u5219\u8df3\u51fa\u5916\u5c42\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u5d4c\u5957\u5faa\u73af\u653e\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u4f7f\u7528 <code>return<\/code> \u6765\u8df3\u51fa\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def nested_loops():<\/p>\n<p>    while True:  # \u5916\u5c42\u5faa\u73af<\/p>\n<p>        while True:  # \u5185\u5c42\u5faa\u73af<\/p>\n<p>            print(&quot;Inside inner loop&quot;)<\/p>\n<p>            # \u67d0\u4e9b\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de<\/p>\n<p>            if condition:<\/p>\n<p>                return<\/p>\n<p>        print(&quot;Inside outer loop&quot;)<\/p>\n<p>nested_loops()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53 <code>condition<\/code> \u6ee1\u8db3\u65f6\uff0c\u4f7f\u7528 <code>return<\/code> \u8df3\u51fa\u51fd\u6570\uff0c\u4ece\u800c\u8df3\u51fa\u6240\u6709\u5d4c\u5957\u7684\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u901a\u8fc7\u629b\u51fa\u548c\u6355\u83b7\u5f02\u5e38\u6765\u8df3\u51fa\u5d4c\u5957\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class BreakLoop(Exception):<\/p>\n<p>    pass<\/p>\n<p>try:<\/p>\n<p>    while True:  # \u5916\u5c42\u5faa\u73af<\/p>\n<p>        while True:  # \u5185\u5c42\u5faa\u73af<\/p>\n<p>            print(&quot;Inside inner loop&quot;)<\/p>\n<p>            # \u67d0\u4e9b\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38<\/p>\n<p>            if condition:<\/p>\n<p>                r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se BreakLoop<\/p>\n<p>        print(&quot;Inside outer loop&quot;)<\/p>\n<p>except BreakLoop:<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53 <code>condition<\/code> \u6ee1\u8db3\u65f6\uff0c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38 <code>BreakLoop<\/code>\uff0c\u7136\u540e\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u6355\u83b7\u8be5\u5f02\u5e38\uff0c\u4ece\u800c\u8df3\u51fa\u6240\u6709\u5d4c\u5957\u7684\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4e09\u79cd\u8df3\u51fa\u5d4c\u5957\u7684 while \u5faa\u73af\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 break\u3001\u4f7f\u7528\u51fd\u6570\u548c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3002<strong>\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 break \u662f\u6700\u5e38\u7528\u548c\u6700\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<strong>\u4f7f\u7528\u51fd\u6570\u548c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u4e5f\u662f\u6709\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4e0d\u5982\u5e26\u6807\u8bb0\u7684 break \u76f4\u89c2\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u7f16\u7a0b\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u8df3\u51fa\u591a\u4e2a\u5d4c\u5957\u7684while\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u8df3\u51fa\u5f53\u524d\u7684while\u5faa\u73af\uff0c\u4f46\u5982\u679c\u60f3\u8981\u8df3\u51fa\u591a\u4e2a\u5d4c\u5957\u7684\u5faa\u73af\uff0c\u901a\u5e38\u9700\u8981\u7ed3\u5408\u6807\u5fd7\u53d8\u91cf\u6216\u5f02\u5e38\u5904\u7406\u7684\u65b9\u6cd5\u3002\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u53ef\u4ee5\u5728\u5916\u5c42\u5faa\u73af\u4e2d\u8fdb\u884c\u5224\u65ad\uff0c\u800c\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u5219\u53ef\u4ee5\u76f4\u63a5\u9000\u51fa\u6240\u6709\u5faa\u73af\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u6280\u5de7\u53ef\u4ee5\u4f7f\u5d4c\u5957\u7684while\u5faa\u73af\u66f4\u6613\u4e8e\u7ba1\u7406\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5d4c\u5957\u7684while\u5faa\u73af\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u903b\u8f91\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\uff0c\u5229\u7528\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6765\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u4e5f\u4fbf\u4e8e\u540e\u671f\u7684\u7ef4\u62a4\u548c\u4fee\u6539\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>break<\/code>\u548c<code>continue<\/code>\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>break<\/code>\u8df3\u51fa\u5faa\u73af\u65f6\uff0c\u786e\u4fdd\u5728\u5408\u9002\u7684\u6761\u4ef6\u4e0b\u4f7f\u7528\u5b83\uff0c\u4ee5\u907f\u514d\u610f\u5916\u7684\u65e9\u671f\u7ec8\u6b62\u3002<code>continue<\/code>\u5219\u53ef\u4ee5\u7528\u6765\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\uff0c\u8fd9\u5728\u5904\u7406\u590d\u6742\u7684\u903b\u8f91\u65f6\u975e\u5e38\u6709\u7528\u3002\u5408\u7406\u4f7f\u7528\u8fd9\u4e24\u4e2a\u63a7\u5236\u8bed\u53e5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u8df3\u51fa\u5d4c\u5957\u7684\u4e24\u4e2a while \u5faa\u73af\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5e26\u6807\u8bb0\u7684 bre [&hellip;]","protected":false},"author":3,"featured_media":1032589,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032582"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1032582"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032582\/revisions"}],"predecessor-version":[{"id":1032591,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032582\/revisions\/1032591"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032589"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}