{"id":1120482,"date":"2025-01-08T18:56:59","date_gmt":"2025-01-08T10:56:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120482.html"},"modified":"2025-01-08T18:57:02","modified_gmt":"2025-01-08T10:57:02","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e4%b8%80%e4%b8%aa%e5%be%aa%e7%8e%af%e5%a4%9a%e6%ac%a1%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120482.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u4e00\u4e2a\u5faa\u73af\u591a\u6b21\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083134\/142862c6-3946-4159-bb22-e7f8f22f5006.webp\" alt=\"python\u5982\u4f55\u8ba9\u4e00\u4e2a\u5faa\u73af\u591a\u6b21\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9Python\u4e2d\u7684\u4e00\u4e2a\u5faa\u73af\u591a\u6b21\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3001<code>while<\/code>\u5faa\u73af\u3001\u9012\u5f52\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<strong><code>while<\/code>\u5faa\u73af<\/strong> \u662f\u6700\u5e38\u89c1\u548c\u7075\u6d3b\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u5faa\u73af\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b83\u6765\u8ba9\u4e00\u4e2a\u5faa\u73af\u591a\u6b21\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001FOR \u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>for<\/code>\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u4e00\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u904d\u5386\u4e00\u4e2a\u5e8f\u5217\uff08\u4f8b\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u8303\u56f4\u6765\u8ba9\u5b83\u591a\u6b21\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):  # \u8fd9\u4e2a\u5faa\u73af\u4f1a\u6267\u884c10\u6b21<\/p>\n<p>    print(&quot;\u8fd9\u662f\u7b2c&quot;, i + 1, &quot;\u6b21\u5faa\u73af&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>range(10)<\/code> \u751f\u6210\u4e00\u4e2a\u4ece0\u52309\u7684\u5e8f\u5217\uff0c<code>for<\/code>\u5faa\u73af\u5c06\u904d\u5386\u8fd9\u4e2a\u5e8f\u5217\uff0c\u5e76\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\u6267\u884c <code>print<\/code> \u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001WHILE \u5faa\u73af<\/h3>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u5b83\u4f1a\u4e00\u76f4\u6267\u884c\u5faa\u73af\u4f53\uff0c\u76f4\u5230\u6307\u5b9a\u7684\u6761\u4ef6\u4e0d\u518d\u6ee1\u8db3\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u5b83\u591a\u6b21\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 10:  # \u8fd9\u4e2a\u5faa\u73af\u4f1a\u6267\u884c10\u6b21<\/p>\n<p>    print(&quot;\u8fd9\u662f\u7b2c&quot;, count + 1, &quot;\u6b21\u5faa\u73af&quot;)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5faa\u73af\u4f53\u4f1a\u4e00\u76f4\u6267\u884c\uff0c\u76f4\u5230 <code>count<\/code> \u4e0d\u518d\u5c0f\u4e8e10\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9012\u5f52<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5faa\u73af\u5f62\u5f0f\uff0c\u5b83\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u5b9e\u73b0\u5faa\u73af\u3002\u9012\u5f52\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u53ef\u4ee5\u88ab\u5206\u89e3\u4e3a\u76f8\u540c\u5b50\u95ee\u9898\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_loop(count, max_count):<\/p>\n<p>    if count &lt; max_count:<\/p>\n<p>        print(&quot;\u8fd9\u662f\u7b2c&quot;, count + 1, &quot;\u6b21\u5faa\u73af&quot;)<\/p>\n<p>        recursive_loop(count + 1, max_count)<\/p>\n<p>recursive_loop(0, 10)  # \u8fd9\u4e2a\u9012\u5f52\u4f1a\u6267\u884c10\u6b21<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>recursive_loop<\/code> \u51fd\u6570\u4f1a\u4e00\u76f4\u8c03\u7528\u81ea\u8eab\uff0c\u76f4\u5230 <code>count<\/code> \u4e0d\u518d\u5c0f\u4e8e <code>max_count<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5d4c\u5957\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u662f\u6307\u5728\u4e00\u4e2a\u5faa\u73af\u4f53\u5185\u518d\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u591a\u5c42\u6b21\u7684\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(3):  # \u5916\u5c42\u5faa\u73af<\/p>\n<p>    for j in range(3):  # \u5185\u5c42\u5faa\u73af<\/p>\n<p>        print(&quot;\u8fd9\u662f\u7b2c&quot;, i + 1, &quot;-&quot;, j + 1, &quot;\u6b21\u5faa\u73af&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5185\u5c42\u5faa\u73af\u4f1a\u5728\u6bcf\u6b21\u5916\u5c42\u5faa\u73af\u6267\u884c\u65f6\u88ab\u6267\u884c3\u6b21\uff0c\u603b\u5171\u4f1a\u6267\u884c9\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u6765\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u65f6\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def countdown_timer(seconds):<\/p>\n<p>    while seconds &gt; 0:<\/p>\n<p>        print(&quot;\u5012\u8ba1\u65f6:&quot;, seconds, &quot;\u79d2&quot;)<\/p>\n<p>        time.sleep(1)  # \u6682\u505c1\u79d2<\/p>\n<p>        seconds -= 1<\/p>\n<p>    print(&quot;\u65f6\u95f4\u5230\uff01&quot;)<\/p>\n<p>countdown_timer(10)  # \u8fd9\u4e2a\u8ba1\u65f6\u5668\u4f1a\u5012\u8ba1\u65f610\u79d2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>countdown_timer<\/code> \u51fd\u6570\u4f1a\u6bcf\u79d2\u949f\u51cf\u5c11\u4e00\u6b21 <code>seconds<\/code>\uff0c\u76f4\u5230\u5b83\u7b49\u4e8e0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5faa\u73af\u63a7\u5236\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u63a7\u5236\u8bed\u53e5\u6765\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u4e24\u4e2a\u5e38\u89c1\u7684\u63a7\u5236\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><h4>1. <code>break<\/code> \u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>break<\/code> \u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(10):<\/p>\n<p>    if i == 5:<\/p>\n<p>        break  # \u5f53i\u7b49\u4e8e5\u65f6\uff0c\u7ec8\u6b62\u5faa\u73af<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <code>continue<\/code> \u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>continue<\/code> \u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u90e8\u5206\uff0c\u5e76\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/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  # \u8df3\u8fc7\u5076\u6570<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u3001<code>while<\/code> \u5faa\u73af\u3001\u9012\u5f52\u3001\u5d4c\u5957\u5faa\u73af\u4ee5\u53ca\u63a7\u5236\u8bed\u53e5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u591a\u6b21\u5faa\u73af\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u9002\u5408\u7684\u5faa\u73af\u7ed3\u6784\u548c\u63a7\u5236\u8bed\u53e5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5d4c\u5957\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5d4c\u5957\u5faa\u73af\u662f\u6307\u5728\u4e00\u4e2a\u5faa\u73af\u5185\u90e8\u518d\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u5faa\u73af\u3002\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8bed\u6cd5\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a\u4f7f\u7528<code>for<\/code>\u6216<code>while<\/code>\u8bed\u53e5\u3002\u5d4c\u5957\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u636e\u7ed3\u6784\u6216\u9700\u8981\u591a\u6b21\u8fed\u4ee3\u7684\u573a\u666f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(3):  # \u5916\u5c42\u5faa\u73af\n    for j in range(2):  # \u5185\u5c42\u5faa\u73af\n        print(i, j)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c06\u6253\u5370\u51fa\u6240\u6709\u7684(i, j)\u7ec4\u5408\u3002<\/p>\n<p><strong>\u5982\u4f55\u63a7\u5236\u5faa\u73af\u7684\u6b21\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>range()<\/code>\u51fd\u6570\u6765\u63a7\u5236\u5faa\u73af\u7684\u6b21\u6570\u3002<code>range(start, stop, step)<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u6570\u5b57\u5e8f\u5217\u3002\u4f8b\u5982\uff0c<code>range(5)<\/code>\u4f1a\u751f\u62100\u52304\u7684\u6570\u5b57\uff0c\u5faa\u73af5\u6b21\u3002\u5728<code>for<\/code>\u5faa\u73af\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5c06<code>range()<\/code>\u7684\u53c2\u6570\u8fdb\u884c\u8c03\u6574\u6765\u6539\u53d8\u5faa\u73af\u6b21\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\uff1f<\/strong><br \/>\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u53ef\u4ee5\u7ed3\u5408<code>if<\/code>\u8bed\u53e5\u4e0e\u5faa\u73af\u8bed\u53e5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    if i % 2 == 0:\n        print(f&quot;{i} \u662f\u5076\u6570&quot;)\n    else:\n        print(f&quot;{i} \u662f\u5947\u6570&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u6253\u5370\u51fa0\u52309\u7684\u6570\u5b57\uff0c\u5e76\u6807\u8bc6\u6bcf\u4e2a\u6570\u5b57\u662f\u5076\u6570\u8fd8\u662f\u5947\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9Python\u4e2d\u7684\u4e00\u4e2a\u5faa\u73af\u591a\u6b21\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u9012\u5f52\u7b49\u65b9\u6cd5\u3002 \u5176\u4e2d\uff0cwhile\u5faa\u73af [&hellip;]","protected":false},"author":3,"featured_media":1120488,"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\/1120482"}],"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=1120482"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120482\/revisions"}],"predecessor-version":[{"id":1120490,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120482\/revisions\/1120490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120488"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}