{"id":967486,"date":"2024-12-27T04:57:36","date_gmt":"2024-12-26T20:57:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/967486.html"},"modified":"2024-12-27T04:57:39","modified_gmt":"2024-12-26T20:57:39","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e5%be%aa%e7%8e%af%e5%bc%80%e5%a7%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/967486.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u5faa\u73af\u5f00\u59cb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182744\/a7a2b021-9b91-4b05-bbfc-e1b3178f1ae5.webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u5faa\u73af\u5f00\u59cb\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u8fd4\u56de\u5faa\u73af\u7684\u5f00\u59cb\uff0c\u53ef\u4ee5\u4f7f\u7528continue\u8bed\u53e5\u3001\u4e2d\u65ad\u5faa\u73af\u540e\u91cd\u65b0\u5f00\u59cb\u5faa\u73af\u3001\u4f7f\u7528\u6807\u5fd7\u4f4d\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0ccontinue\u8bed\u53e5\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u80fd\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u5e76\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528continue\u8bed\u53e5\u4ee5\u53ca\u5176\u4ed6\u65b9\u6cd5\u6765\u5b9e\u73b0\u5faa\u73af\u7684\u91cd\u65b0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528CONTINUE\u8bed\u53e5<\/p>\n<\/p>\n<p><p>continue\u8bed\u53e5\u662fPython\u63d0\u4f9b\u7684\u4e00\u4e2a\u63a7\u5236\u5faa\u73af\u7684\u8bed\u53e5\u3002\u5b83\u7528\u4e8e\u7acb\u5373\u7ed3\u675f\u5f53\u524d\u5faa\u73af\u8fed\u4ee3\uff0c\u5e76\u8df3\u5230\u5faa\u73af\u7684\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u3002\u5728for\u5faa\u73af\u548cwhile\u5faa\u73af\u4e2d\u90fd\u53ef\u4ee5\u4f7f\u7528continue\u8bed\u53e5\u3002<\/p>\n<\/p>\n<ol>\n<li>for\u5faa\u73af\u4e2d\u7684continue<\/li>\n<\/ol>\n<p><p>\u5728for\u5faa\u73af\u4e2d\u4f7f\u7528continue\u8bed\u53e5\uff0c\u53ef\u4ee5\u76f4\u63a5\u8df3\u8fc7\u5f53\u524d\u7684\u5faa\u73af\u4f53\u4e2d\u7684\u5269\u4f59\u8bed\u53e5\uff0c\u8fdb\u5165\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0ccontinue\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5076\u6570\u7684\u6253\u5370\u64cd\u4f5c\uff0c\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u4e3a1, 3, 5, 7, 9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>while\u5faa\u73af\u4e2d\u7684continue<\/li>\n<\/ol>\n<p><p>\u5728while\u5faa\u73af\u4e2d\uff0ccontinue\u8bed\u53e5\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0efor\u5faa\u73af\u76f8\u4f3c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while i &lt; 10:<\/p>\n<p>    i += 1<\/p>\n<p>    if i % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0ccontinue\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5076\u6570\u7684\u6253\u5370\u64cd\u4f5c\uff0c\u56e0\u6b64\u8f93\u51fa\u7ed3\u679c\u4e3a1, 3, 5, 7, 9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528BREAK\u8bed\u53e5\u7ed3\u5408LOOP\u91cd\u542f<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4e2d\u65ad\u5f53\u524d\u5faa\u73af\u5e76\u91cd\u65b0\u5f00\u59cb\u6574\u4e2a\u5faa\u73af\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7break\u8bed\u53e5\u7ed3\u5408\u5faa\u73af\u5916\u90e8\u7684\u6807\u5fd7\u4f4d\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528break\u8bed\u53e5\u91cd\u542f\u5faa\u73af<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528break\u8bed\u53e5\u91cd\u542f\u6574\u4e2a\u5faa\u73af\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    for i in range(10):<\/p>\n<p>        if i == 5:<\/p>\n<p>            print(&quot;Restarting loop&quot;)<\/p>\n<p>            break<\/p>\n<p>        print(i)<\/p>\n<p>    else:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53i\u7b49\u4e8e5\u65f6\uff0cbreak\u8bed\u53e5\u4e2d\u65ad\u4e86for\u5faa\u73af\uff0c\u7136\u540ewhile\u5faa\u73af\u91cd\u65b0\u5f00\u59cb\u3002\u901a\u8fc7\u8fd9\u4e2a\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u5faa\u73af\u7684\u91cd\u542f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6807\u5fd7\u4f4d\u63a7\u5236\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u63a7\u5236\u5faa\u73af\u7684\u91cd\u65b0\u5f00\u59cb\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u5728\u5faa\u73af\u4e2d\u8fdb\u884c\u590d\u6742\u5224\u65ad\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6807\u5fd7\u4f4d\u793a\u4f8b<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u6807\u5fd7\u4f4d\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">restart = True<\/p>\n<p>while restart:<\/p>\n<p>    restart = False<\/p>\n<p>    for i in range(10):<\/p>\n<p>        if i == 5:<\/p>\n<p>            print(&quot;Restarting loop with flag&quot;)<\/p>\n<p>            restart = True<\/p>\n<p>            break<\/p>\n<p>        print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53i\u7b49\u4e8e5\u65f6\uff0c\u6807\u5fd7\u4f4drestart\u88ab\u8bbe\u7f6e\u4e3aTrue\uff0c\u7136\u540ewhile\u5faa\u73af\u91cd\u65b0\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u5faa\u73af\u903b\u8f91<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u60c5\u51b5\u4e0b\uff0c\u5c06\u5faa\u73af\u903b\u8f91\u5c01\u88c5\u5230\u51fd\u6570\u4e2d\uff0c\u8c03\u7528\u51fd\u6570\u6765\u91cd\u542f\u5faa\u73af\u4e5f\u662f\u4e00\u79cd\u53ef\u884c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u51fd\u6570\u5c01\u88c5\u793a\u4f8b<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u5faa\u73af\u903b\u8f91\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def loop_function():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        if i == 5:<\/p>\n<p>            print(&quot;Restarting loop with function&quot;)<\/p>\n<p>            return True<\/p>\n<p>        print(i)<\/p>\n<p>    return False<\/p>\n<p>while loop_function():<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53i\u7b49\u4e8e5\u65f6\uff0c\u51fd\u6570\u8fd4\u56deTrue\uff0c\u7136\u540ewhile\u5faa\u73af\u91cd\u65b0\u8c03\u7528\u51fd\u6570\uff0c\u91cd\u542f\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236Python\u5faa\u73af\u7684\u91cd\u65b0\u5f00\u59cb\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u5faa\u73af\u903b\u8f91\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\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\u63a7\u5236\u5faa\u73af\u7684\u8fd4\u56de\u548c\u8df3\u8f6c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>continue<\/code>\u8bed\u53e5\u6765\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u90e8\u5206\uff0c\u76f4\u63a5\u8fd4\u56de\u5faa\u73af\u7684\u5f00\u59cb\u3002\u8fd9\u610f\u5473\u7740\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u5faa\u73af\u5c06\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u7684\u540e\u7eed\u4ee3\u7801\uff0c\u7ee7\u7eed\u5230\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<p><strong>\u5728Python\u7684\u5faa\u73af\u4e2d\uff0c\u5982\u4f55\u9000\u51fa\u5f53\u524d\u5faa\u73af\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728Python\u5faa\u73af\u4e2d\u7acb\u5373\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u3002\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c<code>break<\/code>\u5c06\u505c\u6b62\u5faa\u73af\u6267\u884c\uff0c\u4e0d\u518d\u8fdb\u884c\u540e\u7eed\u8fed\u4ee3\u3002\u8fd9\u5728\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u63d0\u524d\u9000\u51fa\u5faa\u73af\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5faa\u73af\u5d4c\u5957\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u3002\u4e00\u4e2a\u5faa\u73af\u53ef\u4ee5\u5728\u53e6\u4e00\u4e2a\u5faa\u73af\u5185\u90e8\u6267\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e24\u4e2a<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u904d\u5386\u4e8c\u7ef4\u5217\u8868\u3002\u6ce8\u610f\uff0c\u5728\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u5185\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\u90fd\u4f1a\u5728\u5916\u5c42\u5faa\u73af\u7684\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u6267\u884c\uff0c\u56e0\u6b64\u9700\u8981\u5408\u7406\u8bbe\u7f6e\u5faa\u73af\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u6216\u6027\u80fd\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u8fd4\u56de\u5faa\u73af\u7684\u5f00\u59cb\uff0c\u53ef\u4ee5\u4f7f\u7528continue\u8bed\u53e5\u3001\u4e2d\u65ad\u5faa\u73af\u540e\u91cd\u65b0\u5f00\u59cb\u5faa\u73af\u3001\u4f7f\u7528\u6807\u5fd7\u4f4d\u7b49\u65b9\u6cd5\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":967496,"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\/967486"}],"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=967486"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967486\/revisions"}],"predecessor-version":[{"id":967497,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/967486\/revisions\/967497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/967496"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=967486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=967486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=967486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}