{"id":991825,"date":"2024-12-27T08:32:18","date_gmt":"2024-12-27T00:32:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/991825.html"},"modified":"2024-12-27T08:32:20","modified_gmt":"2024-12-27T00:32:20","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%97%a0%e9%99%90%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/991825.html","title":{"rendered":"\u5982\u4f55\u7528python\u65e0\u9650\u5faa\u73af"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065934\/379aeb1d-8ee5-4f21-a214-713838940824.webp\" alt=\"\u5982\u4f55\u7528python\u65e0\u9650\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while True<\/code>\u8bed\u53e5\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3001\u6216\u4f7f\u7528\u751f\u6210\u5668\u3002<\/strong> \u5176\u4e2d\uff0c<strong><code>while True<\/code><\/strong> \u662f\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528<code>while True<\/code>\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5faa\u73af\uff0c\u76f4\u5230\u67d0\u4e2a\u6761\u4ef6\u88ab\u6ee1\u8db3\u540e\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u8df3\u51fa\u5faa\u73af\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528 <code>while True<\/code><\/p>\n<\/p>\n<p><p><code>while True<\/code> \u662f\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0c<code>while<\/code>\u540e\u9762\u7684\u6761\u4ef6\u59cb\u7ec8\u4e3a<code>True<\/code>\uff0c\u56e0\u6b64\u5faa\u73af\u4f1a\u4e00\u76f4\u6267\u884c\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u5408\u9002\u7684\u5730\u65b9\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u5faa\u73af: &quot;)<\/p>\n<p>    if user_input.lower() == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u7a0b\u5e8f\u5c06\u6301\u7eed\u8fd0\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u201cexit\u201d\u4e3a\u6b62\u3002\u6b64\u65f6\uff0c\u5faa\u73af\u5c06\u901a\u8fc7<code>break<\/code>\u8bed\u53e5\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>while True<\/code> \u7684\u4f18\u52bf\u5728\u4e8e\u7b80\u5355\u548c\u76f4\u89c2\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u82e5\u6ca1\u6709\u5408\u9002\u7684\u9000\u51fa\u6761\u4ef6\uff0c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u505c\u6b62\u3002\u56e0\u6b64\uff0c\u5728\u8bbe\u8ba1\u5faa\u73af\u903b\u8f91\u65f6\uff0c\u5e94\u786e\u4fdd\u603b\u6709\u4e00\u79cd\u65b9\u5f0f\u80fd\u591f\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u53e6\u4e00\u79cd\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u7684\u65b9\u6cd5\u3002\u9012\u5f52\u662f\u6307\u51fd\u6570\u8c03\u7528\u81ea\u8eab\uff0c\u901a\u8fc7\u8bbe\u7f6e\u51fd\u6570\u7684\u9012\u5f52\u6761\u4ef6\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5faa\u73af\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def recursive_loop():<\/p>\n<p>    # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c<\/p>\n<p>    user_input = input(&quot;\u8bf7\u8f93\u5165 &#39;exit&#39; \u9000\u51fa\u9012\u5f52: &quot;)<\/p>\n<p>    if user_input.lower() != &#39;exit&#39;:<\/p>\n<p>        recursive_loop()<\/p>\n<p>recursive_loop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u51fd\u6570<code>recursive_loop<\/code>\u5728\u6bcf\u6b21\u8c03\u7528\u540e\u53c8\u8c03\u7528\u81ea\u5df1\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5faa\u73af\u3002\u4e0e<code>while True<\/code>\u4e0d\u540c\uff0c\u9012\u5f52\u65b9\u5f0f\u66f4\u7075\u6d3b\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u9012\u5f52\u6df1\u5ea6\u7684\u9650\u5236\u4ee5\u53ca\u6f5c\u5728\u7684\u6808\u6ea2\u51fa\u95ee\u9898\u3002\u786e\u4fdd\u5728\u5408\u9002\u7684\u6761\u4ef6\u4e0b\u7ed3\u675f\u9012\u5f52\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668<\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u4e5f\u662f\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u7684\u4e00\u4e2a\u6709\u6548\u5de5\u5177\u3002Python\u4e2d\u7684\u751f\u6210\u5668\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u751f\u6210\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u67d0\u4e9b\u6761\u4ef6\u4e0b\u65e0\u9650\u5730\u4ea7\u751f\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def infinite_generator():<\/p>\n<p>    n = 0<\/p>\n<p>    while True:<\/p>\n<p>        yield n<\/p>\n<p>        n += 1<\/p>\n<p>for num in infinite_generator():<\/p>\n<p>    print(num)<\/p>\n<p>    if num &gt; 10:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u6682\u505c\u5176\u72b6\u6001\uff0c\u76f4\u5230\u518d\u6b21\u88ab\u8c03\u7528\u3002\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u9002\u5408\u9700\u8981\u8fde\u7eed\u751f\u6210\u6570\u636e\u7684\u573a\u666f\u3002\u901a\u8fc7\u5728\u5916\u90e8\u6761\u4ef6\u63a7\u5236\u4f55\u65f6\u505c\u6b62\u8fed\u4ee3\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4f7f\u7528\u751f\u6210\u5668\u6765\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u65e0\u9650\u5faa\u73af\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec<code>while True<\/code>\u3001\u9012\u5f52\u51fd\u6570\u548c\u751f\u6210\u5668\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\u3002\u5728\u4f7f\u7528<code>while True<\/code>\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u9000\u51fa\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u9677\u5165\u6b7b\u5faa\u73af\u3002\u5728\u4f7f\u7528\u9012\u5f52\u65f6\uff0c\u8981\u5c0f\u5fc3\u9012\u5f52\u6df1\u5ea6\u548c\u6808\u6ea2\u51fa\u7684\u95ee\u9898\u3002\u800c\u751f\u6210\u5668\u5728\u9700\u8981\u8fde\u7eed\u751f\u6210\u6570\u636e\u7684\u573a\u5408\u8868\u73b0\u51fa\u8272\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5faa\u73af\u7684\u7ed3\u675f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u6240\u9700\u7684\u5faa\u73af\u903b\u8f91\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\u53ef\u4ee5\u901a\u8fc7<code>while<\/code>\u8bed\u53e5\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while True:<\/code>\u6765\u521b\u5efa\u4e00\u4e2a\u59cb\u7ec8\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u8fd9\u6837\u5faa\u73af\u5c31\u4f1a\u4e0d\u65ad\u6267\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    print(&quot;\u8fd9\u662f\u4e00\u6761\u65e0\u9650\u5faa\u73af\u7684\u6d88\u606f&quot;)\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u65f6\u8981\u5c0f\u5fc3\uff0c\u786e\u4fdd\u5728\u5408\u9002\u7684\u6761\u4ef6\u4e0b\u9000\u51fa\u5faa\u73af\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u65e0\u54cd\u5e94\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b89\u5168\u5730\u7ec8\u6b62\u65e0\u9650\u5faa\u73af\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u5b89\u5168\u5730\u9000\u51fa\u65e0\u9650\u5faa\u73af\u3002\u5728\u5faa\u73af\u5185\u90e8\u8bbe\u7f6e\u4e00\u4e2a\u6761\u4ef6\uff0c\u4f8b\u5982\u6839\u636e\u7528\u6237\u8f93\u5165\u6765\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8f93\u5165&#39;quit&#39;\u4ee5\u9000\u51fa\u5faa\u73af\uff1a&quot;)\n    if user_input.lower() == &#39;quit&#39;:\n        break\n    print(&quot;\u5faa\u73af\u7ee7\u7eed&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u5faa\u73af\u7684\u7ec8\u6b62\uff0c\u907f\u514d\u7a0b\u5e8f\u88ab\u9501\u6b7b\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u65f6\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5e38\u89c1\u9519\u8bef\u5305\u62ec\u672a\u8bbe\u7f6e\u9000\u51fa\u6761\u4ef6\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u54cd\u5e94\uff0c\u4ee5\u53ca\u5728\u5faa\u73af\u4e2d\u672a\u5904\u7406\u5f02\u5e38\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u786e\u4fdd\u5728\u5faa\u73af\u5185\u90e8\u52a0\u5165\u5fc5\u8981\u7684\u5f02\u5e38\u5904\u7406\uff0c\u4f8b\u5982\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u6765\u6355\u83b7\u6f5c\u5728\u7684\u9519\u8bef\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u5728\u5faa\u73af\u5185\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u548c\u76d1\u63a7\u7a0b\u5e8f\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\uff0c\u53ef\u4ee5\u4f7f\u7528while True\u8bed\u53e5\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3001\u6216\u4f7f\u7528\u751f\u6210\u5668\u3002 \u5176\u4e2d\uff0cw [&hellip;]","protected":false},"author":3,"featured_media":991831,"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\/991825"}],"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=991825"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991825\/revisions"}],"predecessor-version":[{"id":991832,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991825\/revisions\/991832"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/991831"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=991825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=991825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=991825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}