{"id":1013216,"date":"2024-12-27T11:46:02","date_gmt":"2024-12-27T03:46:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013216.html"},"modified":"2024-12-27T11:46:04","modified_gmt":"2024-12-27T03:46:04","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e9%80%80%e5%87%bawhile","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1013216.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u9000\u51fawhile"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090728\/afc86467-99dc-4d6e-9d17-25fab6b0a618.webp\" alt=\"python\u4e2d\u5982\u4f55\u9000\u51fawhile\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u9000\u51fawhile\u5faa\u73af\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528break\u8bed\u53e5\u3001\u6ee1\u8db3\u6761\u4ef6\u8df3\u51fa\u5faa\u73af\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528break\u8bed\u53e5<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u76f4\u63a5\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u5faa\u73af\u4f53\u5185\u8bbe\u7f6e\u6761\u4ef6\u5224\u65ad\uff0c\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u6267\u884cbreak\u8bed\u53e5\uff0c\u5373\u53ef\u4e2d\u65ad\u5faa\u73af\u3002\u8be6\u7ec6\u6765\u8bf4\uff0c\u901a\u8fc7\u5728while\u5faa\u73af\u5185\u90e8\u5b9e\u73b0\u903b\u8f91\u5224\u65ad\uff0c\u5982\u679c\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u6267\u884cbreak\u8bed\u53e5\u6765\u7ec8\u6b62\u5faa\u73af\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528BREAK\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528break\u8bed\u53e5\u662f\u9000\u51fawhile\u5faa\u73af\u7684\u76f4\u63a5\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0cbreak\u8bed\u53e5\u7528\u4e8e\u7ec8\u6b62\u5faa\u73af\u8bed\u53e5\u7684\u6267\u884c\u5e76\u8df3\u51fa\u5faa\u73af\u4f53\u3002\u5b83\u5e38\u7528\u4e8e\u9700\u8981\u5728\u5faa\u73af\u4e2d\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u63d0\u524d\u9000\u51fa\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u4f55\u65f6\u4f7f\u7528break\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5728while\u5faa\u73af\u4e2d\uff0c\u5f53\u4f60\u5e0c\u671b\u5728\u67d0\u4e2a\u6761\u4ef6\u6ee1\u8db3\u65f6\u7acb\u5373\u9000\u51fa\u5faa\u73af\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u67d0\u4e2a\u7279\u5b9a\u5b57\u7b26\u6216\u5b57\u7b26\u4e32\u65f6\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>1.2 break\u8bed\u53e5\u7684\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        print(&quot;\u5faa\u73af\u5df2\u9000\u51fa&quot;)<\/p>\n<p>        break<\/p>\n<p>    print(&quot;\u4f60\u8f93\u5165\u7684\u662f:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cwhile\u5faa\u73af\u4f1a\u65e0\u9650\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165&#39;exit&#39;\u5b57\u7b26\u4e32\u65f6\uff0cbreak\u8bed\u53e5\u6267\u884c\uff0c\u5faa\u73af\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>1.3 \u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528break\u8bed\u53e5\u65f6\u8981\u6ce8\u610f\uff0cbreak\u53ea\u4f1a\u4e2d\u65ad\u5f53\u524d\u7684\u5faa\u73af\uff0c\u5982\u679c\u6709\u5d4c\u5957\u5faa\u73af\uff0cbreak\u53ea\u4f1a\u8df3\u51fa\u5185\u5c42\u7684\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u6761\u4ef6\u63a7\u5236\u9000\u51fa\u5faa\u73af<\/p>\n<\/p>\n<p><h3>2.1 \u901a\u8fc7\u6761\u4ef6\u63a7\u5236\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528break\u8bed\u53e5\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5faa\u73af\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002\u4f8b\u5982\uff0c\u5728\u5faa\u73af\u5f00\u59cb\u65f6\u8bbe\u7f6e\u4e00\u4e2a\u53d8\u91cf\u4f5c\u4e3a\u5faa\u73af\u6761\u4ef6\uff0c\u5e76\u5728\u5faa\u73af\u5185\u90e8\u6839\u636e\u903b\u8f91\u4fee\u6539\u8be5\u53d8\u91cf\u4ee5\u63a7\u5236\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">continue_loop = True<\/p>\n<p>while continue_loop:<\/p>\n<p>    user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        continue_loop = False<\/p>\n<p>        print(&quot;\u5faa\u73af\u5df2\u9000\u51fa&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;\u4f60\u8f93\u5165\u7684\u662f:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4e00\u4e2a\u5e03\u5c14\u53d8\u91cf<code>continue_loop<\/code>\u4f5c\u4e3awhile\u5faa\u73af\u7684\u6761\u4ef6\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>2.3 \u4f18\u70b9\u4e0e\u5c40\u9650<\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u66f4\u5bb9\u6613\u7406\u89e3\u548c\u63a7\u5236\u7a0b\u5e8f\u7684\u6d41\u7a0b\uff0c\u5c24\u5176\u662f\u5728\u590d\u6742\u7684\u5faa\u73af\u903b\u8f91\u4e2d\u3002\u4f46\u4e0ebreak\u76f8\u6bd4\uff0c\u5b83\u7684\u4ee3\u7801\u53ef\u80fd\u7a0d\u663e\u5197\u957f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u9000\u51fa\u5faa\u73af<\/p>\n<\/p>\n<p><h3>3.1 \u5f02\u5e38\u5904\u7406\u5728\u5faa\u73af\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u7528\u4e8e\u9000\u51fa\u5faa\u73af\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5916\u90e8\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\uff09\u65f6\uff0c\u901a\u8fc7\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\u6765\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>        if user_input == &#39;exit&#39;:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration(&quot;\u7528\u6237\u8bf7\u6c42\u9000\u51fa&quot;)<\/p>\n<p>        print(&quot;\u4f60\u8f93\u5165\u7684\u662f:&quot;, user_input)<\/p>\n<p>except StopIteration as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u629b\u51fa\u5e76\u6355\u83b7\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38<code>StopIteration<\/code>\u6765\u5b9e\u73b0\u5faa\u73af\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><h3>3.3 \u4f7f\u7528\u573a\u666f\u53ca\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u9002\u7528\u4e8e\u9700\u8981\u5904\u7406\u9519\u8bef\u6216\u7279\u6b8a\u9000\u51fa\u6761\u4ef6\u7684\u573a\u666f\uff0c\u9700\u6ce8\u610f\u5728\u4f7f\u7528\u5f02\u5e38\u65f6\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528RETURN\u8bed\u53e5\u9000\u51fa\u5faa\u73af<\/p>\n<\/p>\n<p><h3>4.1 \u5728\u51fd\u6570\u4e2d\u4f7f\u7528return\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u5982\u679cwhile\u5faa\u73af\u5728\u4e00\u4e2a\u51fd\u6570\u5185\uff0c\u4f7f\u7528return\u8bed\u53e5\u53ef\u4ee5\u9000\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u51fd\u6570\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def process_input():<\/p>\n<p>    while True:<\/p>\n<p>        user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>        if user_input == &#39;exit&#39;:<\/p>\n<p>            return &quot;\u5faa\u73af\u5df2\u9000\u51fa&quot;<\/p>\n<p>        print(&quot;\u4f60\u8f93\u5165\u7684\u662f:&quot;, user_input)<\/p>\n<p>result = process_input()<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0creturn\u8bed\u53e5\u4e0d\u4ec5\u9000\u51fa\u4e86\u5faa\u73af\uff0c\u4e5f\u7ed3\u675f\u4e86\u51fd\u6570\u7684\u6267\u884c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>4.3 \u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528return\u8bed\u53e5\u9000\u51fa\u5faa\u73af\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8fd4\u56de\u503c\u7684\u51fd\u6570\u4e2d\uff0c\u80fd\u591f\u7b80\u5316\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u9000\u51fawhile\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c<strong>break\u8bed\u53e5<\/strong>\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff1b<strong>\u6761\u4ef6\u63a7\u5236<\/strong>\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u9002\u5408\u4e8e\u9700\u8981\u590d\u6742\u903b\u8f91\u5224\u65ad\u7684\u60c5\u51b5\uff1b<strong>\u5f02\u5e38\u5904\u7406<\/strong>\u9002\u5408\u4e8e\u5916\u90e8\u8d44\u6e90\u7684\u7ba1\u7406\uff1b<strong>return\u8bed\u53e5<\/strong>\u9002\u7528\u4e8e\u51fd\u6570\u5185\u90e8\u7684\u5faa\u73af\u9000\u51fa\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u4ee3\u7801\u903b\u8f91\u590d\u6742\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f18\u96c5\u5730\u9000\u51fawhile\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u4f18\u96c5\u5730\u9000\u51fawhile\u5faa\u73af\u3002\u5f53\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\uff0c\u6267\u884c<code>break<\/code>\u53ef\u4ee5\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\u3002\u786e\u4fdd\u5728\u5faa\u73af\u4f53\u5185\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\u6765\u89e6\u53d1<code>break<\/code>\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u5faa\u73af\u7684\u53d1\u751f\u3002<\/p>\n<p><strong>\u5728while\u5faa\u73af\u4e2d\uff0c\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u9000\u51fa\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\uff0c\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\u6765\u63a7\u5236\u5faa\u73af\u7684\u9000\u51fa\u3002\u5f53\u6807\u5fd7\u53d8\u91cf\u88ab\u4fee\u6539\u4e3a\u7279\u5b9a\u503c\u65f6\uff0cwhile\u5faa\u73af\u4f1a\u6839\u636e\u8be5\u6761\u4ef6\u505c\u6b62\u6267\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u5e76\u4e3a\u5faa\u73af\u7684\u9000\u51fa\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406while\u5faa\u73af\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\u4ee5\u786e\u4fdd\u5b89\u5168\u9000\u51fa\uff1f<\/strong><br \/>\u5728while\u5faa\u73af\u4e2d\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u6f5c\u5728\u7684\u5f02\u5e38\uff0c\u4ece\u800c\u4fdd\u8bc1\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3002\u5f53\u53d1\u751f\u5f02\u5e38\u65f6\uff0c\u53ef\u4ee5\u5728<code>except<\/code>\u5757\u4e2d\u6dfb\u52a0<code>break<\/code>\u8bed\u53e5\uff0c\u4ee5\u786e\u4fdd\u5faa\u73af\u5728\u9047\u5230\u9519\u8bef\u65f6\u80fd\u591f\u5b89\u5168\u9000\u51fa\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u6216\u8fdb\u5165\u65e0\u9650\u5faa\u73af\u7684\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u9000\u51fawhile\u5faa\u73af\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u4f7f\u7528break\u8bed\u53e5\u3001\u6ee1\u8db3\u6761\u4ef6\u8df3\u51fa\u5faa\u73af\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u3002\u5176\u4e2d\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1013225,"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\/1013216"}],"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=1013216"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013216\/revisions"}],"predecessor-version":[{"id":1013226,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1013216\/revisions\/1013226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1013225"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1013216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1013216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1013216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}