{"id":999521,"date":"2024-12-27T09:42:15","date_gmt":"2024-12-27T01:42:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999521.html"},"modified":"2024-12-27T09:42:32","modified_gmt":"2024-12-27T01:42:32","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9while%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999521.html","title":{"rendered":"python\u5982\u4f55\u8ba9while\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074503\/8e325a0c-5656-4eca-8b9d-0d2ea1f4a4ff.webp\" alt=\"python\u5982\u4f55\u8ba9while\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u6307\u5b9a\u7684\u6761\u4ef6\u4e3a\u5047\u3002\u8981\u8ba9<code>while<\/code>\u5faa\u73af\u6b63\u5e38\u5de5\u4f5c\uff0c\u9700\u8981\u786e\u4fdd\uff1a\u660e\u786e\u7684\u5faa\u73af\u6761\u4ef6\u3001\u5408\u7406\u7684\u5faa\u73af\u4f53\u5185\u5bb9\u3001\u907f\u514d\u65e0\u9650\u5faa\u73af\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u5faa\u73af\u4f53\u5185\u4fee\u6539\u6761\u4ef6\u53d8\u91cf\uff0c\u6dfb\u52a0\u7528\u6237\u8f93\u5165\u6216\u5f15\u5165\u8ba1\u6570\u5668\u53d8\u91cf\u6765\u63a7\u5236\u5faa\u73af\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u660e\u786e\u7684\u5faa\u73af\u6761\u4ef6<\/strong>\u662f\u6307\u5728\u7f16\u5199<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u5fc5\u987b\u6709\u4e00\u4e2a\u660e\u786e\u7684\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4e00\u4e2a\u8ba1\u6570\u5668\u53d8\u91cf\u6765\u786e\u4fdd\u5faa\u73af\u53ea\u8fd0\u884c\u7279\u5b9a\u6b21\u6570\u3002<strong>\u5408\u7406\u7684\u5faa\u73af\u4f53\u5185\u5bb9<\/strong>\u662f\u6307\u5728\u5faa\u73af\u5185\u6267\u884c\u7684\u4ee3\u7801\u5e94\u4e0e\u5faa\u73af\u7684\u76ee\u7684\u76f8\u4e00\u81f4\uff0c\u5e76\u4e14\u4e0d\u5e94\u5305\u542b\u4e0d\u5fc5\u8981\u7684\u64cd\u4f5c\u3002<strong>\u907f\u514d\u65e0\u9650\u5faa\u73af<\/strong>\u662f\u6307\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u80fd\u591f\u5728\u67d0\u4e2a\u65f6\u95f4\u70b9\u53d8\u4e3a\u5047\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u5361\u5728\u65e0\u9650\u5faa\u73af\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u660e\u786e\u7684\u5faa\u73af\u6761\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u6700\u91cd\u8981\u7684\u662f\u786e\u4fdd\u6709\u4e00\u4e2a\u660e\u786e\u7684\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002\u901a\u5e38\uff0c\u8fd9\u4e2a\u6761\u4ef6\u53ef\u4ee5\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5f53\u8be5\u8868\u8fbe\u5f0f\u4e3a<code>True<\/code>\u65f6\uff0c\u5faa\u73af\u5c06\u7ee7\u7eed\u6267\u884c\uff1b\u5f53\u5b83\u4e3a<code>False<\/code>\u65f6\uff0c\u5faa\u73af\u5c06\u505c\u6b62\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u8ba1\u6570\u5668\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u8ba1\u6570\u5668\u53d8\u91cf\u3002\u8ba1\u6570\u5668\u53d8\u91cf\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u90fd\u4f1a\u9012\u589e\uff08\u6216\u9012\u51cf\uff09\uff0c\u5e76\u5728\u8fbe\u5230\u67d0\u4e2a\u503c\u65f6\u4f7f\u5faa\u73af\u505c\u6b62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(&quot;This is iteration number&quot;, count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5faa\u73af\u5c06\u5728<code>count<\/code>\u8fbe\u52305\u65f6\u505c\u6b62\u3002<\/p>\n<\/p>\n<p><h4>2. \u57fa\u4e8e\u7528\u6237\u8f93\u5165<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u6839\u636e\u7528\u6237\u7684\u8f93\u5165\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u6cd5\u5e38\u7528\u4e8e\u4ea4\u4e92\u5f0f\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = &#39;&#39;<\/p>\n<p>while user_input.lower() != &#39;exit&#39;:<\/p>\n<p>    user_input = input(&quot;Enter something (type &#39;exit&#39; to quit): &quot;)<\/p>\n<p>    print(&quot;You entered:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u5faa\u73af\u4f1a\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165&#39;exit&#39;\u4e3a\u6b62\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5408\u7406\u7684\u5faa\u73af\u4f53\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u903b\u8f91\u4e0e\u5faa\u73af\u7684\u76ee\u7684\u76f8\u7b26\u3002\u907f\u514d\u5728\u5faa\u73af\u4f53\u5185\u8fdb\u884c\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u6216\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1. \u7b80\u5316\u5faa\u73af\u4f53<\/h4>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u5c3d\u91cf\u4fdd\u6301\u5faa\u73af\u4f53\u7b80\u5355\u660e\u4e86\u3002\u590d\u6742\u7684\u903b\u8f91\u5bb9\u6613\u5bfc\u81f4\u9519\u8bef\uff0c\u4e14\u5f71\u54cd\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>index = 0<\/p>\n<p>while index &lt; len(numbers):<\/p>\n<p>    print(numbers[index])<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5faa\u73af\u4f53\u4ec5\u5305\u542b\u6253\u5370\u5217\u8868\u5143\u7d20\u548c\u589e\u52a0\u7d22\u5f15\u7684\u7b80\u5355\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u67d0\u4e2a\u590d\u6742\u8ba1\u7b97\u7684\u7ed3\u679c\uff0c\u8003\u8651\u5728\u5faa\u73af\u5916\u8fdb\u884c\u8ba1\u7b97\uff0c\u5e76\u5c06\u7ed3\u679c\u4f20\u5165\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>value = 100<\/p>\n<p>sqrt_value = math.sqrt(value)<\/p>\n<p>while value &gt; 0:<\/p>\n<p>    print(&quot;Square root of&quot;, value, &quot;is&quot;, sqrt_value)<\/p>\n<p>    value -= 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u5faa\u73af\u5916\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u6211\u4eec\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u907f\u514d\u65e0\u9650\u5faa\u73af<\/h3>\n<\/p>\n<p><p><strong>\u907f\u514d\u65e0\u9650\u5faa\u73af<\/strong>\u662f<code>while<\/code>\u5faa\u73af\u8bbe\u8ba1\u4e2d\u7684\u5173\u952e\u539f\u5219\u4e4b\u4e00\u3002\u65e0\u9650\u5faa\u73af\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u54cd\u5e94\uff0c\u5e76\u53ef\u80fd\u5f15\u53d1\u7cfb\u7edf\u8d44\u6e90\u8017\u5c3d\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u53ef\u53d8<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u65e0\u9650\u5faa\u73af\uff0c\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u80fd\u591f\u5728\u5faa\u73af\u4f53\u5185\u88ab\u4fee\u6539\uff0c\u4f7f\u5f97\u6761\u4ef6\u6700\u7ec8\u53d8\u4e3a<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 10<\/p>\n<p>while number &gt; 0:<\/p>\n<p>    print(number)<\/p>\n<p>    number -= 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>number<\/code>\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u9012\u51cf\uff0c\u6700\u7ec8\u4f7f\u6761\u4ef6<code>number &gt; 0<\/code>\u4e3a<code>False<\/code>\uff0c\u4ece\u800c\u7ed3\u675f\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>break<\/code>\u8bed\u53e5<\/h4>\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\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;Type &#39;quit&#39; to exit: &quot;)<\/p>\n<p>    if user_input.lower() == &#39;quit&#39;:<\/p>\n<p>        break<\/p>\n<p>    print(&quot;You typed:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u901a\u8fc7\u68c0\u6d4b\u7528\u6237\u8f93\u5165\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>continue<\/code>\u548c<code>break<\/code><\/h3>\n<\/p>\n<p><p>Python\u7684<code>while<\/code>\u5faa\u73af\u652f\u6301<code>continue<\/code>\u548c<code>break<\/code>\u8bed\u53e5\uff0c\u5b83\u4eec\u53ef\u4ee5\u5206\u522b\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u6216\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h4>1. <code>continue<\/code>\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u5728\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u65f6\u8df3\u8fc7\u5269\u4f59\u7684\u5faa\u73af\u4f53\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 0<\/p>\n<p>while counter &lt; 10:<\/p>\n<p>    counter += 1<\/p>\n<p>    if counter % 2 == 0:<\/p>\n<p>        continue<\/p>\n<p>    print(&quot;Odd number:&quot;, counter)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>continue<\/code>\u8bed\u53e5\u4f7f\u5f97\u5076\u6570\u4e0d\u88ab\u6253\u5370\u3002<\/p>\n<\/p>\n<p><h4>2. <code>break<\/code>\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u4e0d\u518d\u6267\u884c\u5269\u4f59\u7684\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 0<\/p>\n<p>while counter &lt; 10:<\/p>\n<p>    print(counter)<\/p>\n<p>    if counter == 5:<\/p>\n<p>        break<\/p>\n<p>    counter += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>break<\/code>\u8bed\u53e5\u5728<code>counter<\/code>\u7b49\u4e8e5\u65f6\u7ec8\u6b62\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5d4c\u5957<code>while<\/code>\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5728\u4e00\u4e2a<code>while<\/code>\u5faa\u73af\u4e2d\u5d4c\u5957\u53e6\u4e00\u4e2a<code>while<\/code>\u5faa\u73af\uff0c\u79f0\u4e3a\u5d4c\u5957<code>while<\/code>\u5faa\u73af\u3002\u8fd9\u5728\u9700\u8981\u591a\u5c42\u6b21\u8fed\u4ee3\u65f6\u5f88\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u7840\u5d4c\u5957\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5d4c\u5957<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\u6216\u77e9\u9635\u7b49\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while i &lt; 3:<\/p>\n<p>    j = 0<\/p>\n<p>    while j &lt; 3:<\/p>\n<p>        print(f&quot;i: {i}, j: {j}&quot;)<\/p>\n<p>        j += 1<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\u7684\u5d4c\u5957\u5faa\u73af\u4f1a\u6253\u5370\u6bcf\u5bf9<code>i<\/code>\u548c<code>j<\/code>\u7684\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><h4>2. \u63a7\u5236\u5d4c\u5957\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u5728\u5d4c\u5957<code>while<\/code>\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u548c<code>continue<\/code>\u8bed\u53e5\u6765\u63a7\u5236\u5185\u5c42\u5faa\u73af\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">i = 0<\/p>\n<p>while i &lt; 3:<\/p>\n<p>    j = 0<\/p>\n<p>    while j &lt; 3:<\/p>\n<p>        if i == j:<\/p>\n<p>            break<\/p>\n<p>        print(f&quot;i: {i}, j: {j}&quot;)<\/p>\n<p>        j += 1<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53<code>i<\/code>\u7b49\u4e8e<code>j<\/code>\u65f6\uff0c\u5185\u5c42\u5faa\u73af\u5c06\u88ab\u4e2d\u65ad\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5728<code>while<\/code>\u5faa\u73af\u4e2d\u5904\u7406\u5f02\u5e38<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c<code>while<\/code>\u5faa\u73af\u4e2d\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u800c\u4e0d\u5f71\u54cd\u5faa\u73af\u7684\u6574\u4f53\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u5f02\u5e38\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u5728<code>while<\/code>\u5faa\u73af\u4e2d\u6355\u83b7\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        num = random.randint(1, 10)<\/p>\n<p>        result = 10 \/ num<\/p>\n<p>        print(f&quot;10 divided by {num} is {result}&quot;)<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        print(&quot;Division by zero occurred, skipping...&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ZeroDivisionError<\/code>\u88ab\u6355\u83b7\u5e76\u5904\u7406\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>2. \u6355\u83b7\u591a\u79cd\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u6355\u83b7\u591a\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\uff0c\u5e76\u5bf9\u4e0d\u540c\u7684\u5f02\u5e38\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        user_input = input(&quot;Enter a number: &quot;)<\/p>\n<p>        number = int(user_input)<\/p>\n<p>        print(&quot;You entered:&quot;, number)<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;That&#39;s not a valid number, try ag<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.&quot;)<\/p>\n<p>    except KeyboardInterrupt:<\/p>\n<p>        print(&quot;Interrupted by user, exiting...&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>ValueError<\/code>\u548c<code>KeyboardInterrupt<\/code>\u88ab\u5206\u522b\u6355\u83b7\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316<code>while<\/code>\u5faa\u73af\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u5c3d\u91cf\u4f18\u5316\u5faa\u73af\u6027\u80fd\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6574\u4f53\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1. \u51cf\u5c11\u5faa\u73af\u4f53\u5185\u7684\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u907f\u514d\u5728\u5faa\u73af\u4f53\u5185\u8fdb\u884c\u91cd\u590d\u8ba1\u7b97\u6216\u8c03\u7528\u5f00\u9500\u8f83\u5927\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [i for i in range(1000000)]<\/p>\n<p>index = 0<\/p>\n<p>data_length = len(data)  # \u8ba1\u7b97\u4e00\u6b21\u957f\u5ea6<\/p>\n<p>while index &lt; data_length:<\/p>\n<p>    # \u5728\u5faa\u73af\u4f53\u5185\u4e0d\u518d\u91cd\u590d\u8ba1\u7b97\u957f\u5ea6<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5728\u5faa\u73af\u5916\u8ba1\u7b97\u6570\u636e\u957f\u5ea6\uff0c\u53ef\u4ee5\u51cf\u5c11\u5faa\u73af\u4f53\u5185\u7684\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f18\u5316\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8<code>while<\/code>\u5faa\u73af\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u96c6\u5408\u800c\u4e0d\u662f\u5217\u8868\u6765\u8fdb\u884c\u6210\u5458\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">elements = set([1, 2, 3, 4, 5])  # \u4f7f\u7528\u96c6\u5408\u800c\u975e\u5217\u8868<\/p>\n<p>index = 0<\/p>\n<p>while index &lt; 10:<\/p>\n<p>    if index in elements:<\/p>\n<p>        print(f&quot;{index} is in elements&quot;)<\/p>\n<p>    index += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u96c6\u5408\u8fdb\u884c\u6210\u5458\u6d4b\u8bd5\u901a\u5e38\u6bd4\u5217\u8868\u66f4\u5feb\uff0c\u5c24\u5176\u662f\u5728\u5927\u578b\u6570\u636e\u96c6\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u5728Python\u7f16\u7a0b\u4e2d\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u91cd\u590d\u6267\u884c\u4ee3\u7801\u5757\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1\u5faa\u73af\u6761\u4ef6\u3001\u7b80\u5316\u5faa\u73af\u4f53\u3001\u907f\u514d\u65e0\u9650\u5faa\u73af\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u4ee5\u53ca\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5229\u7528<code>while<\/code>\u5faa\u73af\u6765\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u548c\u7b97\u6cd5\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5c06\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u548c\u53ef\u9760\u7684\u4ee3\u7801\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\u7684while\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199<code>while True:<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u7684while\u5faa\u73af\u3002\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u5c06\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u4f60\u624b\u52a8\u7ec8\u6b62\u7a0b\u5e8f\uff08\u4f8b\u5982\uff0c\u901a\u8fc7Ctrl+C\uff09\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u786e\u4fdd\u5728\u5faa\u73af\u4e2d\u6709\u6761\u4ef6\u53ef\u4ee5\u8df3\u51fa\u5faa\u73af\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u9677\u5165\u6b7b\u5faa\u73af\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728while\u5faa\u73af\u4e2d\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\uff1f<\/strong><br \/>\u4f60\u53ef\u4ee5\u5728while\u5faa\u73af\u7684\u6761\u4ef6\u90e8\u5206\u653e\u7f6e\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>while x &lt; 10:<\/code>\u53ef\u4ee5\u8ba9\u5faa\u73af\u5728<code>x<\/code>\u5c0f\u4e8e10\u65f6\u7ee7\u7eed\u6267\u884c\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u4f60\u53ef\u4ee5\u4fee\u6539<code>x<\/code>\u7684\u503c\uff0c\u4ece\u800c\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\u7ec8\u6b62\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u5f0f\u5e38\u7528\u4e8e\u9700\u8981\u6839\u636e\u52a8\u6001\u53d8\u5316\u7684\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728while\u5faa\u73af\u4e2d\u4f7f\u7528break\u548ccontinue\u8bed\u53e5\uff1f<\/strong><br \/>\u5728while\u5faa\u73af\u4e2d\uff0c<code>break<\/code>\u8bed\u53e5\u53ef\u4ee5\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u800c<code>continue<\/code>\u8bed\u53e5\u5219\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u5f53\u9700\u8981\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u7ec8\u6b62\u6216\u8df3\u8fc7\u67d0\u4e9b\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u8fd9\u4e24\u4e2a\u8bed\u53e5\u975e\u5e38\u6709\u6548\u3002\u4f8b\u5982\uff0c\u5f53\u4e00\u4e2a\u8f93\u5165\u4e0d\u7b26\u5408\u8981\u6c42\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>continue<\/code>\u8df3\u8fc7\u5904\u7406\uff0c\u6216\u8005\u4f7f\u7528<code>break<\/code>\u7ec8\u6b62\u6574\u4e2a\u5faa\u73af\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cwhile\u5faa\u73af\u7528\u4e8e\u91cd\u590d\u6267\u884c\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u76f4\u5230\u6307\u5b9a\u7684\u6761\u4ef6\u4e3a\u5047\u3002\u8981\u8ba9while\u5faa\u73af\u6b63\u5e38\u5de5\u4f5c\uff0c\u9700\u8981\u786e [&hellip;]","protected":false},"author":3,"featured_media":999555,"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\/999521"}],"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=999521"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999521\/revisions"}],"predecessor-version":[{"id":999557,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999521\/revisions\/999557"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999555"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}