{"id":974834,"date":"2024-12-27T06:08:24","date_gmt":"2024-12-26T22:08:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974834.html"},"modified":"2024-12-27T06:08:26","modified_gmt":"2024-12-26T22:08:26","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%be%aa%e7%8e%af%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974834.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u5faa\u73af\u8bed\u53e5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200342\/8fdbba7f-a002-46d0-8209-c9e9e6d5ed7f.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u5faa\u73af\u8bed\u53e5\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8bbe\u7f6e\u5faa\u73af\u8bed\u53e5\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<strong>for\u5faa\u73af\u3001while\u5faa\u73af<\/strong>\u3002<strong>for\u5faa\u73af<\/strong>\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff0c\u5982\u5217\u8868\u6216\u5b57\u7b26\u4e32\uff0c<strong>while\u5faa\u73af<\/strong>\u5219\u7528\u4e8e\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\u53cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002<strong>for\u5faa\u73af<\/strong>\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u5faa\u73af\u6b21\u6570\u7684\u60c5\u51b5\uff0c\u800c<strong>while\u5faa\u73af<\/strong>\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u7528\u4e8e\u9700\u8981\u6839\u636e\u52a8\u6001\u6761\u4ef6\u51b3\u5b9a\u5faa\u73af\u6b21\u6570\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u5faa\u73af\u8bed\u53e5\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001FOR\u5faa\u73af<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u8bed\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>for\u5faa\u73af<\/strong>\u7528\u4e8e\u904d\u5386\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001\u5b57\u7b26\u4e32\u7b49\uff09\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for element in iterable:<\/p>\n<p>    # \u6267\u884c\u8bed\u53e5\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c<code>element<\/code>\u662f\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u7528\u4e8e\u5b58\u50a8\u5f53\u524d\u8fed\u4ee3\u5230\u7684\u5143\u7d20\uff0c<code>iterable<\/code>\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h2>2. \u904d\u5386\u5217\u8868<\/h2>\n<\/p>\n<p><p><strong>for\u5faa\u73af<\/strong>\u5e38\u7528\u4e8e\u904d\u5386\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u904d\u5386\u4e00\u4e2a\u6570\u5b57\u5217\u8868\uff0c\u5e76\u6253\u5370\u6bcf\u4e2a\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>for number in numbers:<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u4f9d\u6b21\u5c06<code>numbers<\/code>\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u7ed9\u53d8\u91cf<code>number<\/code>\uff0c\u5e76\u5728\u5faa\u73af\u4f53\u4e2d\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>3. \u904d\u5386\u5b57\u7b26\u4e32<\/h2>\n<\/p>\n<p><p><strong>for\u5faa\u73af<\/strong>\u4e5f\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello&quot;<\/p>\n<p>for char in text:<\/p>\n<p>    print(char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u4f9d\u6b21\u5c06\u5b57\u7b26\u4e32<code>text<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8d4b\u503c\u7ed9\u53d8\u91cf<code>char<\/code>\uff0c\u5e76\u5728\u5faa\u73af\u4f53\u4e2d\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>4. \u4f7f\u7528range()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u8fdb\u884c\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528<code>range()<\/code>\u51fd\u6570\u3002<code>range()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff0c\u5e38\u4e0e<code>for<\/code>\u5faa\u73af\u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>range(5)<\/code>\u751f\u6210\u4e00\u4e2a\u4ece0\u52304\uff08\u4e0d\u5305\u62ec5\uff09\u7684\u6574\u6570\u5e8f\u5217\uff0c<code>for<\/code>\u5faa\u73af\u5c06\u6bcf\u4e2a\u6574\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf<code>i<\/code>\uff0c\u5e76\u5728\u5faa\u73af\u4f53\u4e2d\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h2>5. \u4f7f\u7528enumerate()\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u904d\u5386\u5143\u7d20\u7684\u540c\u65f6\u83b7\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>enumerate()<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for index, fruit in enumerate(fruits):<\/p>\n<p>    print(index, fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>enumerate(fruits)<\/code>\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7d22\u5f15\u548c\u503c\u7684\u5143\u7ec4\uff0c<code>for<\/code>\u5faa\u73af\u5c06\u5176\u89e3\u5305\u4e3a<code>index<\/code>\u548c<code>fruit<\/code>\u4e24\u4e2a\u53d8\u91cf\uff0c\u5e76\u5728\u5faa\u73af\u4f53\u4e2d\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001WHILE\u5faa\u73af<\/strong><\/h2>\n<p><h2>1. \u57fa\u672c\u8bed\u6cd5<\/h2>\n<\/p>\n<p><p><strong>while\u5faa\u73af<\/strong>\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\u53cd\u590d\u6267\u884c\u4ee3\u7801\u5757\uff0c\u5176\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # \u6267\u884c\u8bed\u53e5\u5757<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c<code>condition<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u4e3a\u771f\uff0c\u5219\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><h2>2. \u5e38\u89c1\u7528\u6cd5<\/h2>\n<\/p>\n<p><p><strong>while\u5faa\u73af<\/strong>\u9002\u7528\u4e8e\u5728\u672a\u77e5\u5faa\u73af\u6b21\u6570\u7684\u60c5\u51b5\u4e0b\u6267\u884c\u4ee3\u7801\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6253\u5370\u4ece1\u52305\u7684\u6570\u5b57\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 1<\/p>\n<p>while count &lt;= 5:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u5728<code>count<\/code>\u5c0f\u4e8e\u7b49\u4e8e5\u65f6\u6267\u884c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5757\uff0c\u6bcf\u6b21\u5faa\u73af\u540e<code>count<\/code>\u9012\u589e1\u3002<\/p>\n<\/p>\n<p><h2>3. \u65e0\u9650\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u5982\u679c<code>while<\/code>\u5faa\u73af\u7684\u6761\u4ef6\u59cb\u7ec8\u4e3a\u771f\uff0c\u5219\u4f1a\u5bfc\u81f4\u65e0\u9650\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65e0\u9650\u5faa\u73af\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    print(&quot;This is an infinite loop.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u7684\u6761\u4ef6\u59cb\u7ec8\u4e3a<code>True<\/code>\uff0c\u56e0\u6b64\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u5757\u5c06\u65e0\u9650\u6b21\u6267\u884c\u3002\u901a\u5e38\u9700\u8981\u5728\u5faa\u73af\u4f53\u4e2d\u52a0\u5165\u4e2d\u65ad\u6761\u4ef6\uff08\u5982<code>break<\/code>\u8bed\u53e5\uff09\u6765\u7ec8\u6b62\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h2>4. \u4f7f\u7528break\u8bed\u53e5<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u5728\u5faa\u73af\u4e2d\u9014\u9000\u51fa\u5faa\u73af\u3002\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while True:<\/p>\n<p>    print(count)<\/p>\n<p>    count += 1<\/p>\n<p>    if count &gt;= 5:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u5728<code>count<\/code>\u5927\u4e8e\u7b49\u4e8e5\u65f6\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h2>5. \u4f7f\u7528continue\u8bed\u53e5<\/h2>\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\u7acb\u5373\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u8fed\u4ee3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(5):<\/p>\n<p>    if i == 2:<\/p>\n<p>        continue<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7<code>i<\/code>\u7b49\u4e8e2\u65f6\u7684\u5faa\u73af\u8fed\u4ee3\uff0c\u56e0\u6b64\u6253\u5370\u7ed3\u679c\u4e2d\u6ca1\u6709\u6570\u5b572\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5d4c\u5957\u5faa\u73af<\/strong><\/h2>\n<p><h2>1. \u5d4c\u5957FOR\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u5faa\u73af\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u5e38\u7528\u4e8e\u5904\u7406\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff08\u5982\u4e8c\u7ef4\u5217\u8868\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5d4c\u5957<code>for<\/code>\u5faa\u73af\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>for row in matrix:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42<code>for<\/code>\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u5217\u8868<code>matrix<\/code>\u4e2d\u7684\u6bcf\u4e00\u884c\uff0c\u5185\u5c42<code>for<\/code>\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>2. \u5d4c\u5957WHILE\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957<code>while<\/code>\u5faa\u73af\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u591a\u5c42\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5d4c\u5957<code>while<\/code>\u5faa\u73af\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">outer = 1<\/p>\n<p>while outer &lt;= 3:<\/p>\n<p>    inner = 1<\/p>\n<p>    while inner &lt;= 3:<\/p>\n<p>        print(f&quot;Outer: {outer}, Inner: {inner}&quot;)<\/p>\n<p>        inner += 1<\/p>\n<p>    outer += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5916\u5c42<code>while<\/code>\u5faa\u73af\u63a7\u5236<code>outer<\/code>\u53d8\u91cf\u7684\u9012\u589e\uff0c\u5185\u5c42<code>while<\/code>\u5faa\u73af\u63a7\u5236<code>inner<\/code>\u53d8\u91cf\u7684\u9012\u589e\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5faa\u73af\u7684\u5e94\u7528\u573a\u666f<\/strong><\/h2>\n<p><h2>1. \u6570\u636e\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5faa\u73af\u8bed\u53e5\u5728\u6570\u636e\u5904\u7406\u4efb\u52a1\u4e2d\u975e\u5e38\u5e38\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e00\u9879\uff0c\u5e76\u5e94\u7528\u67d0\u79cd\u8f6c\u6362\u6216\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 2, 3, 4, 5]<\/p>\n<p>squared_data = []<\/p>\n<p>for x in data:<\/p>\n<p>    squared_data.append(x  2)<\/p>\n<p>print(squared_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u7528\u4e8e\u5c06\u5217\u8868<code>data<\/code>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e73\u65b9\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728<code>squared_data<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>2. \u641c\u7d22\u7b97\u6cd5<\/h2>\n<\/p>\n<p><p>\u5faa\u73af\u8bed\u53e5\u4e5f\u5e38\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u641c\u7d22\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u5b9e\u73b0\u7ebf\u6027\u641c\u7d22\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def linear_search(arr, target):<\/p>\n<p>    index = 0<\/p>\n<p>    while index &lt; len(arr):<\/p>\n<p>        if arr[index] == target:<\/p>\n<p>            return index<\/p>\n<p>        index += 1<\/p>\n<p>    return -1<\/p>\n<p>numbers = [4, 2, 7, 1, 3]<\/p>\n<p>result = linear_search(numbers, 7)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u7528\u4e8e\u904d\u5386<code>numbers<\/code>\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u627e\u5230\u76ee\u6807\u503c\u65f6\u8fd4\u56de\u5176\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h2>3. \u6e38\u620f\u5f00\u53d1<\/h2>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u5faa\u73af\u8bed\u53e5\u7528\u4e8e\u5b9e\u73b0\u6e38\u620f\u4e3b\u5faa\u73af\u3002\u6e38\u620f\u4e3b\u5faa\u73af\u8d1f\u8d23\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u66f4\u65b0\u6e38\u620f\u72b6\u6001\u548c\u6e32\u67d3\u6e38\u620f\u753b\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">running = True<\/p>\n<p>while running:<\/p>\n<p>    # \u5904\u7406\u7528\u6237\u8f93\u5165<\/p>\n<p>    # \u66f4\u65b0\u6e38\u620f\u72b6\u6001<\/p>\n<p>    # \u6e32\u67d3\u6e38\u620f\u753b\u9762<\/p>\n<p>    # \u6761\u4ef6\u5224\u65ad\u7ed3\u675f\u5faa\u73af<\/p>\n<p>    if game_over:<\/p>\n<p>        running = False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u7528\u4e8e\u4e0d\u65ad\u6267\u884c\u6e38\u620f\u7684\u5404\u4e2a\u6b65\u9aa4\uff0c\u76f4\u5230\u6e38\u620f\u7ed3\u675f\u6761\u4ef6\u6ee1\u8db3\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h2>4. \u6a21\u62df\u548c\u5efa\u6a21<\/h2>\n<\/p>\n<p><p>\u5faa\u73af\u8bed\u53e5\u5728\u6a21\u62df\u548c\u5efa\u6a21\u4efb\u52a1\u4e2d\u4e5f\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6a21\u62df\u629b\u786c\u5e01\u5b9e\u9a8c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def coin_flip_simulation(n):<\/p>\n<p>    heads, t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls = 0, 0<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        if random.choice([&#39;H&#39;, &#39;T&#39;]) == &#39;H&#39;:<\/p>\n<p>            heads += 1<\/p>\n<p>        else:<\/p>\n<p>            tails += 1<\/p>\n<p>    return heads, tails<\/p>\n<p>flips = 1000<\/p>\n<p>heads, tails = coin_flip_simulation(flips)<\/p>\n<p>print(f&quot;Heads: {heads}, Tails: {tails}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>for<\/code>\u5faa\u73af\u7528\u4e8e\u6a21\u62df\u591a\u6b21\u629b\u786c\u5e01\u5b9e\u9a8c\uff0c\u5e76\u7edf\u8ba1\u6b63\u9762\u548c\u53cd\u9762\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u5faa\u73af\u4f18\u5316\u6280\u5de7<\/strong><\/h2>\n<p><h2>1. \u51cf\u5c11\u5faa\u73af\u4f53\u5185\u7684\u8ba1\u7b97<\/h2>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u4f53\u5185\u7684\u8ba1\u7b97\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u8d1f\u9762\u5f71\u54cd\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e0d\u53d8\u7684\u8ba1\u7b97\u79fb\u5230\u5faa\u73af\u5916\u90e8\u6765\u4f18\u5316\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u975e\u4f18\u5316\u4ee3\u7801<\/p>\n<p>for i in range(1000):<\/p>\n<p>    result = i * (2 + 3)<\/p>\n<p>    print(result)<\/p>\n<h2><strong>\u4f18\u5316\u4ee3\u7801<\/strong><\/h2>\n<p>constant = 2 + 3<\/p>\n<p>for i in range(1000):<\/p>\n<p>    result = i * constant<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>2 + 3<\/code>\u7684\u8ba1\u7b97\u79fb\u5230\u4e86\u5faa\u73af\u5916\u90e8\uff0c\u51cf\u5c11\u4e86\u5faa\u73af\u4f53\u5185\u7684\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u751f\u6210\u5668\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_numbers(n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        yield i<\/p>\n<p>for number in generate_numbers(1000):<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>generate_numbers<\/code>\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u8bed\u53e5\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u907f\u514d\u4e86\u4e00\u6b21\u6027\u52a0\u8f7d\u5927\u91cf\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>3. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8bbe\u8ba1\u7b97\u6cd5\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u800c\u4e0d\u662f\u624b\u52a8\u68c0\u67e5\u91cd\u590d\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u975e\u4f18\u5316\u4ee3\u7801<\/p>\n<p>unique_numbers = []<\/p>\n<p>for number in numbers:<\/p>\n<p>    if number not in unique_numbers:<\/p>\n<p>        unique_numbers.append(number)<\/p>\n<h2><strong>\u4f18\u5316\u4ee3\u7801<\/strong><\/h2>\n<p>unique_numbers = list(set(numbers))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u904d\u5386\u5217\u8868\u68c0\u67e5\u91cd\u590d\u9879\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1Python\u4e2d\u7684\u5faa\u73af\u8bed\u53e5\u53ca\u5176\u4f18\u5316\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u7f16\u7a0b\u4efb\u52a1\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u8bed\u53e5\u3002\u53ea\u9700\u5c06\u6761\u4ef6\u8bbe\u7f6e\u4e3a<code>True<\/code>\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    # \u6267\u884c\u7684\u4ee3\u7801\n<\/code><\/pre>\n<p>\u8bf7\u52a1\u5fc5\u5728\u5faa\u73af\u5185\u90e8\u5305\u542b\u4e00\u4e2a\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u65e0\u6cd5\u505c\u6b62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<p><strong>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5faa\u73af\u7c7b\u578b\uff1f<\/strong><br \/>Python\u4e3b\u8981\u6709\u4e24\u79cd\u5faa\u73af\u7c7b\u578b\uff1a<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002<code>for<\/code>\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\uff09\uff0c\u800c<code>while<\/code>\u5faa\u73af\u5219\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u6267\u884c\u3002\u9009\u62e9\u54ea\u79cd\u5faa\u73af\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\uff0c\u4f8b\u5982\uff1a\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u65f6\u4f7f\u7528<code>for<\/code>\u5faa\u73af\uff0c\u800c\u9700\u8981\u6839\u636e\u6761\u4ef6\u52a8\u6001\u6267\u884c\u65f6\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5faa\u73af\u4e2d\u4f7f\u7528<code>break<\/code>\u548c<code>continue<\/code>\u8bed\u53e5\uff1f<\/strong><br \/>\u5728Python\u7684\u5faa\u73af\u4e2d\uff0c<code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u800c<code>continue<\/code>\u8bed\u53e5\u5219\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u7ee7\u7eed\u6267\u884c\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u5728<code>for<\/code>\u5faa\u73af\u4e2d\uff0c\u5982\u679c\u9047\u5230\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u7ed3\u675f\u5faa\u73af\uff1b\u5982\u679c\u5e0c\u671b\u8df3\u8fc7\u67d0\u4e9b\u7279\u5b9a\u6761\u4ef6\u4e0b\u7684\u8fed\u4ee3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>continue<\/code>\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">for i in range(10):\n    if i == 5:\n        break  # \u7ec8\u6b62\u5faa\u73af\n    if i % 2 == 0:\n        continue  # \u8df3\u8fc7\u5076\u6570\n    print(i)  # \u53ea\u6253\u5370\u5947\u6570\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8bbe\u7f6e\u5faa\u73af\u8bed\u53e5\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u5f0f\uff1afor\u5faa\u73af\u3001while\u5faa\u73af\u3002for\u5faa\u73af\u7528\u4e8e\u904d\u5386\u5e8f\u5217\uff0c\u5982\u5217\u8868\u6216\u5b57\u7b26 [&hellip;]","protected":false},"author":3,"featured_media":974841,"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\/974834"}],"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=974834"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974834\/revisions"}],"predecessor-version":[{"id":974843,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974834\/revisions\/974843"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974841"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}