{"id":963568,"date":"2024-12-27T04:19:25","date_gmt":"2024-12-26T20:19:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/963568.html"},"modified":"2024-12-27T04:19:28","modified_gmt":"2024-12-26T20:19:28","slug":"python%e5%a6%82%e4%bd%95%e8%b7%b3%e5%87%bawhile%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/963568.html","title":{"rendered":"python\u5982\u4f55\u8df3\u51fawhile\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180808\/a0d4567a-3643-4a67-9713-875c05a1a96f.webp\" alt=\"python\u5982\u4f55\u8df3\u51fawhile\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528break\u3001\u8bbe\u7f6e\u6761\u4ef6\u53d8\u91cf\u3001\u4f7f\u7528return\u8bed\u53e5\u7b49\u65b9\u5f0f\u8df3\u51fawhile\u5faa\u73af\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528break\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5728\u5faa\u73af\u4f53\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\uff0c\u5f53\u6ee1\u8db3\u8be5\u6761\u4ef6\u65f6\uff0c\u6267\u884cbreak\u8bed\u53e5\u6765\u8df3\u51fa\u5faa\u73af\u3002\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\uff0c\u5f53\u8be5\u53d8\u91cf\u5728\u5faa\u73af\u4f53\u4e2d\u8fbe\u5230\u67d0\u4e00\u7279\u5b9a\u72b6\u6001\u65f6\uff0c\u6539\u53d8\u5faa\u73af\u6761\u4ef6\u4ece\u800c\u9000\u51fa\u5faa\u73af\u3002\u6b64\u5916\uff0c\u82e5while\u5faa\u73af\u5728\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7return\u8bed\u53e5\u76f4\u63a5\u8fd4\u56de\u4ece\u800c\u7ec8\u6b62\u5faa\u73af\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528BREAK\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528break\u8bed\u53e5\u662fPython\u4e2d\u8df3\u51fawhile\u5faa\u73af\u7684\u76f4\u63a5\u65b9\u6cd5\u4e4b\u4e00\u3002break\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u5e76\u8df3\u51fa\u5faa\u73af\u4f53\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728while\u5faa\u73af\u4f53\u5185\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\uff0c\u5f53\u8be5\u6761\u4ef6\u4e3a\u771f\u65f6\uff0c\u6267\u884cbreak\u8bed\u53e5\u4ece\u800c\u8df3\u51fa\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165&#39;q&#39;\u4ee5\u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>    if user_input == &#39;q&#39;:<\/p>\n<p>        break<\/p>\n<p>    print(f&quot;\u60a8\u8f93\u5165\u7684\u662f: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u5176\u4ed6\u63a7\u5236\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>break\u8bed\u53e5\u901a\u5e38\u4e0eif\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 0<\/p>\n<p>while counter &lt; 10:<\/p>\n<p>    print(f&quot;\u5f53\u524d\u8ba1\u6570: {counter}&quot;)<\/p>\n<p>    if counter == 5:<\/p>\n<p>        print(&quot;\u8ba1\u6570\u8fbe\u52305\uff0c\u8df3\u51fa\u5faa\u73af&quot;)<\/p>\n<p>        break<\/p>\n<p>    counter += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\u6709\u9009\u62e9\u5730\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u6761\u4ef6\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u662f\u63a7\u5236while\u5faa\u73af\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\uff0c\u901a\u8fc7\u5728\u5faa\u73af\u4f53\u4e2d\u6539\u53d8\u8be5\u53d8\u91cf\u7684\u503c\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5e03\u5c14\u53d8\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\u6765\u51b3\u5b9a\u5faa\u73af\u662f\u5426\u7ee7\u7eed\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">running = True<\/p>\n<p>while running:<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165&#39;exit&#39;\u4ee5\u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        running = False<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;\u60a8\u8f93\u5165\u7684\u662f: {user_input}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u8ba1\u6570\u5668<\/strong><\/li>\n<\/ol>\n<p><p>\u9664\u4e86\u5e03\u5c14\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u8ba1\u6570\u5668\u6765\u63a7\u5236\u5faa\u73af\u7684\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 0<\/p>\n<p>max_count = 5<\/p>\n<p>while counter &lt; max_count:<\/p>\n<p>    print(f&quot;\u5f53\u524d\u8ba1\u6570: {counter}&quot;)<\/p>\n<p>    counter += 1<\/p>\n<p>    if counter &gt;= max_count:<\/p>\n<p>        print(&quot;\u8ba1\u6570\u8fbe\u5230\u6700\u5927\u503c\uff0c\u9000\u51fa\u5faa\u73af&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u8fbe\u5230\u67d0\u4e2a\u8ba1\u6570\u503c\u65f6\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528RETURN\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528return\u8bed\u53e5\u53ef\u4ee5\u76f4\u63a5\u7ec8\u6b62\u51fd\u6570\u7684\u6267\u884c\uff0c\u4ece\u800c\u8df3\u51fawhile\u5faa\u73af\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528return\u8bed\u53e5\u53ef\u4ee5\u7acb\u5373\u7ed3\u675f\u51fd\u6570\u7684\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_value():<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u6216&#39;q&#39;\u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>        if user_input == &#39;q&#39;:<\/p>\n<p>            return<\/p>\n<p>        print(f&quot;\u60a8\u8f93\u5165\u7684\u662f: {user_input}&quot;)<\/p>\n<p>find_value()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u6761\u4ef6\u5224\u65ad<\/strong><\/li>\n<\/ol>\n<p><p>return\u8bed\u53e5\u901a\u5e38\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u4f7f\u7528\uff0c\u4ee5\u51b3\u5b9a\u662f\u5426\u7ec8\u6b62\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def search_list(target, values):<\/p>\n<p>    index = 0<\/p>\n<p>    while index &lt; len(values):<\/p>\n<p>        if values[index] == target:<\/p>\n<p>            print(f&quot;\u627e\u5230\u76ee\u6807\u503c: {target}\uff0c\u5728\u4f4d\u7f6e: {index}&quot;)<\/p>\n<p>            return<\/p>\n<p>        index += 1<\/p>\n<p>    print(&quot;\u76ee\u6807\u503c\u4e0d\u5728\u5217\u8868\u4e2d&quot;)<\/p>\n<p>values_list = [1, 3, 5, 7, 9]<\/p>\n<p>search_list(5, values_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u627e\u5230\u76ee\u6807\u503c\u65f6\u7acb\u5373\u9000\u51fa\u5faa\u73af\u548c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528EXCEPTION\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5229\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u8df3\u51fawhile\u5faa\u73af\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u629b\u51fa\u5f02\u5e38\u5e76\u5728\u5916\u5c42\u6355\u83b7\u6765\u8df3\u51fa\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u6216&#39;q&#39;\u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>        if user_input == &#39;q&#39;:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration<\/p>\n<p>        print(f&quot;\u60a8\u8f93\u5165\u7684\u662f: {user_input}&quot;)<\/p>\n<p>except StopIteration:<\/p>\n<p>    print(&quot;\u5faa\u73af\u5df2\u9000\u51fa&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u5f02\u5e38\u7c7b\u578b\u4ee5\u66f4\u7cbe\u786e\u5730\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ExitLoop(Exception):<\/p>\n<p>    pass<\/p>\n<p>try:<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff0c\u6216&#39;q&#39;\u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>        if user_input == &#39;q&#39;:<\/p>\n<p>            raise ExitLoop<\/p>\n<p>        print(f&quot;\u60a8\u8f93\u5165\u7684\u662f: {user_input}&quot;)<\/p>\n<p>except ExitLoop:<\/p>\n<p>    print(&quot;\u5faa\u73af\u5df2\u9000\u51fa&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8df3\u51fawhile\u5faa\u73af\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528break\u8bed\u53e5\u3002\u901a\u8fc7\u7ed3\u5408\u6761\u4ef6\u53d8\u91cf\u3001return\u8bed\u53e5\u4ee5\u53ca\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u4ee3\u7801\u903b\u8f91\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u4ece\u800c\u7f16\u5199\u51fa\u66f4\u9ad8\u6548\u548c\u5065\u58ee\u7684\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u5168\u5730\u9000\u51fawhile\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b89\u5168\u5730\u9000\u51fawhile\u5faa\u73af\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u3002\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u60a8\u53ef\u4ee5\u5728\u5faa\u73af\u4f53\u5185\u8c03\u7528<code>break<\/code>\uff0c\u8fd9\u4f1a\u7acb\u5373\u7ed3\u675f\u5faa\u73af\u5e76\u8df3\u51fa\u3002\u786e\u4fdd\u5728\u5faa\u73af\u4e2d\u6709\u6761\u4ef6\u5224\u65ad\uff0c\u4ee5\u4fbf\u5728\u5408\u9002\u7684\u65f6\u673a\u9000\u51fa\u3002<\/p>\n<p><strong>\u5728while\u5faa\u73af\u4e2d\u5982\u4f55\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u6765\u63a7\u5236\u9000\u51fa\uff1f<\/strong><br \/>\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u6280\u5de7\u3002\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf\uff0c\u4f8b\u5982<code>running<\/code>\uff0c\u5e76\u5728\u5faa\u73af\u6761\u4ef6\u4e2d\u4f7f\u7528\u5b83\u3002\u5f53\u60a8\u5e0c\u671b\u9000\u51fa\u5faa\u73af\u65f6\uff0c\u53ea\u9700\u5c06\u8be5\u53d8\u91cf\u8bbe\u7f6e\u4e3a<code>False<\/code>\uff0c\u5faa\u73af\u5c06\u4f1a\u505c\u6b62\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u6ee1\u8db3\u591a\u4e2a\u6761\u4ef6\u65f6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u9000\u51fawhile\u5faa\u73af\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u9000\u51fawhile\u5faa\u73af\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u60a8\u53ef\u4ee5\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u5f15\u53d1\u5f02\u5e38\uff0c\u5e76\u5728\u5faa\u73af\u5916\u90e8\u6355\u83b7\u5b83\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u590d\u6742\u7684\u7a0b\u5e8f\u903b\u8f91\uff0c\u5141\u8bb8\u60a8\u5728\u53d1\u751f\u9519\u8bef\u6216\u7279\u5b9a\u4e8b\u4ef6\u65f6\u9000\u51fa\u5faa\u73af\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528break\u3001\u8bbe\u7f6e\u6761\u4ef6\u53d8\u91cf\u3001\u4f7f\u7528return\u8bed\u53e5\u7b49\u65b9\u5f0f\u8df3\u51fawhile\u5faa\u73af\u3002\u5176\u4e2d\uff0c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":963571,"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\/963568"}],"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=963568"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963568\/revisions"}],"predecessor-version":[{"id":963572,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/963568\/revisions\/963572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/963571"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=963568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=963568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=963568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}