{"id":1117993,"date":"2025-01-08T18:31:12","date_gmt":"2025-01-08T10:31:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117993.html"},"modified":"2025-01-08T18:31:15","modified_gmt":"2025-01-08T10:31:15","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e4%b8%80%e4%b8%aa%e6%97%a0%e9%99%90%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1117993.html","title":{"rendered":"python\u5982\u4f55\u5199\u4e00\u4e2a\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\/25081700\/2acb8b3e-dbe0-46d1-8b8d-799b9aaa0fc3.webp\" alt=\"python\u5982\u4f55\u5199\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u7f16\u5199\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5305\u62ec\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3001<code>for<\/code>\u5faa\u73af\u548c\u9012\u5f52\u51fd\u6570\u3002<\/strong> \u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>while True<\/code>\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u3001\u4ee5\u53ca\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u3002\u6700\u5e38\u89c1\u4e14\u5bb9\u6613\u7406\u89e3\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>while True<\/code>\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong><code>while True<\/code>\u5faa\u73af\u662f\u7f16\u5199\u65e0\u9650\u5faa\u73af\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/strong> \u4f7f\u7528<code>while True<\/code>\u5faa\u73af\u65f6\uff0c\u53ea\u9700\u786e\u4fdd\u5728\u67d0\u4e2a\u6761\u4ef6\u4e0b\u53ef\u4ee5\u5b89\u5168\u5730\u9000\u51fa\u5faa\u73af\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u5faa\u73af\u72b6\u6001\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u6267\u884c\u4e00\u4e9b\u4efb\u52a1<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&quot;)<\/p>\n<p>    # \u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u9000\u51fa\u6761\u4ef6<\/p>\n<p>    if some_condition:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while True<\/code>\u4f7f\u5faa\u73af\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u9047\u5230<code>break<\/code>\u8bed\u53e5\u6216\u5176\u4ed6\u9000\u51fa\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528<code>while True<\/code>\u5faa\u73af<\/h2>\n<\/p>\n<p><p><code>while True<\/code>\u5faa\u73af\u662fPython\u4e2d\u521b\u5efa\u65e0\u9650\u5faa\u73af\u7684\u6700\u7b80\u5355\u548c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u66f4\u8be6\u7ec6\u7684\u793a\u4f8b\u548c\u89e3\u91ca\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&quot;)<\/p>\n<p>    # \u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u9000\u51fa\u6761\u4ef6<\/p>\n<p>    user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5faa\u73af\u5c06\u4e00\u76f4\u8fd0\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165&#39;exit&#39;\u4e3a\u6b62\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u6613\u4e8e\u7406\u89e3\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5e94\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u76f4\u89c2<\/strong>\uff1a\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u5728\u5faa\u73af\u5185\u90e8\u6dfb\u52a0\u590d\u6742\u7684\u903b\u8f91\u548c\u9000\u51fa\u6761\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bb9\u6613\u5bfc\u81f4\u6b7b\u5faa\u73af<\/strong>\uff1a\u5982\u679c\u6ca1\u6709\u6b63\u786e\u5904\u7406\u9000\u51fa\u6761\u4ef6\uff0c\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u6c38\u8fdc\u8fd0\u884c\u4e0b\u53bb\u3002<\/li>\n<li><strong>\u8d44\u6e90\u6d88\u8017<\/strong>\uff1a\u65e0\u9650\u5faa\u73af\u4f1a\u5360\u7528\u7cfb\u7edf\u8d44\u6e90\uff0c\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u9664\u4e86<code>while True<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u521b\u5efa\u65e0\u9650\u5faa\u73af\u3002\u4e0d\u8fc7\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u5e38\u89c1\uff0c\u56e0\u4e3a\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\uff08Stack Overflow\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def infinite_recursion():<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u9012\u5f52\u65e0\u9650\u5faa\u73af&quot;)<\/p>\n<p>    infinite_recursion()<\/p>\n<p>infinite_recursion()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u3002<strong>\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5e38\u89c1\uff0c\u4f46\u5b83\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u63d0\u4f9b\u4e00\u79cd\u6709\u8da3\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u72ec\u7279\u6027<\/strong>\uff1a\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u9012\u5f52\u65b9\u6cd5\u53ef\u80fd\u662f\u552f\u4e00\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<li><strong>\u903b\u8f91\u7b80\u6d01<\/strong>\uff1a\u5bf9\u4e8e\u67d0\u4e9b\u9012\u5f52\u95ee\u9898\uff0c\u4ee3\u7801\u53ef\u80fd\u66f4\u7b80\u6d01\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6808\u6ea2\u51fa\u98ce\u9669<\/strong>\uff1a\u9012\u5f52\u6df1\u5ea6\u6709\u9650\uff0c\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\u5dee<\/strong>\uff1a\u4e0d\u5982<code>while True<\/code>\u76f4\u89c2\u3002<\/li>\n<\/ol>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c<code>for<\/code>\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5668\u51fd\u6570\u548c<code>for<\/code>\u5faa\u73af\u6765\u521b\u5efa\u65e0\u9650\u5faa\u73af\u3002\u751f\u6210\u5668\u662fPython\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u51fd\u6570\uff0c\u5b83\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u5e76\u5728\u9700\u8981\u65f6\u6682\u505c\u548c\u6062\u590d\u5176\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def infinite_generator():<\/p>\n<p>    while True:<\/p>\n<p>        yield &quot;\u8fd9\u662f\u4e00\u4e2a\u751f\u6210\u5668\u65e0\u9650\u5faa\u73af&quot;<\/p>\n<p>for value in infinite_generator():<\/p>\n<p>    print(value)<\/p>\n<p>    # \u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u9000\u51fa\u6761\u4ef6<\/p>\n<p>    user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668\u51fd\u6570<code>infinite_generator<\/code>\u6bcf\u6b21\u88ab\u8c03\u7528\u65f6\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u503c\uff0c\u5e76\u4e14<code>for<\/code>\u5faa\u73af\u5c06\u65e0\u9650\u671f\u5730\u8fed\u4ee3\u751f\u6210\u7684\u503c\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u6bd4<code>while True<\/code>\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7075\u6d3b\u6027\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4f18\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u8282\u7701\u5185\u5b58<\/strong>\uff1a\u751f\u6210\u5668\u5728\u9700\u8981\u65f6\u624d\u751f\u6210\u503c\uff0c\u4e0d\u4f1a\u4e00\u6b21\u6027\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u751f\u6210\u5668\u7684\u884c\u4e3a\u548c\u8f93\u51fa\u3002<\/li>\n<\/ol>\n<p><h3>\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u590d\u6742\u6027\u589e\u52a0<\/strong>\uff1a\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u4e0d\u5982<code>while True<\/code>\u76f4\u89c2\u3002<\/li>\n<li><strong>\u9002\u7528\u8303\u56f4\u6709\u9650<\/strong>\uff1a\u4e3b\u8981\u9002\u7528\u4e8e\u9700\u8981\u9010\u6b65\u751f\u6210\u503c\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><h2>\u56db\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u65e0\u9650\u5faa\u73af\u53ef\u4ee5\u7528\u4e8e\u591a\u79cd\u573a\u666f\uff0c\u5305\u62ec\u670d\u52a1\u5668\u76d1\u542c\u3001\u5b9e\u65f6\u6570\u636e\u5904\u7406\u3001\u6e38\u620f\u5faa\u73af\u7b49\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><h3>1\u3001\u786e\u4fdd\u6709\u5408\u7406\u7684\u9000\u51fa\u6761\u4ef6<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u521b\u5efa\u65e0\u9650\u5faa\u73af\uff0c\u90fd\u5e94\u8be5\u786e\u4fdd\u6709\u5408\u7406\u7684\u9000\u51fa\u6761\u4ef6\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u8f93\u5165\u3001\u65f6\u95f4\u9650\u5236\u6216\u5176\u4ed6\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u7ec8\u6b62\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u65e0\u9650\u5faa\u73af\u65f6\uff0c\u9700\u8981\u8003\u8651\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        # \u6267\u884c\u4e00\u4e9b\u4efb\u52a1<\/p>\n<p>        print(&quot;\u8fd9\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&quot;)<\/p>\n<p>        # \u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u9000\u51fa\u6761\u4ef6<\/p>\n<p>        user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>        if user_input == &#39;exit&#39;:<\/p>\n<p>            break<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(f&quot;\u53d1\u751f\u9519\u8bef: {e}&quot;)<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f18\u5316\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u65e0\u9650\u5faa\u73af\u53ef\u80fd\u4f1a\u5360\u7528\u5927\u91cf\u7cfb\u7edf\u8d44\u6e90\uff0c\u56e0\u6b64\u9700\u8981\u6ce8\u610f\u6027\u80fd\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.sleep<\/code>\u51fd\u6570\u5728\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u540e\u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff0c\u51cf\u5c11CPU\u7684\u4f7f\u7528\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>while True:<\/p>\n<p>    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&quot;)<\/p>\n<p>    # \u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u9000\u51fa\u6761\u4ef6<\/p>\n<p>    user_input = input(&quot;\u8f93\u5165 &#39;exit&#39; \u4ee5\u9000\u51fa: &quot;)<\/p>\n<p>    if user_input == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p>    # \u6682\u505c1\u79d2<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u65e0\u9650\u5faa\u73af\u65f6\uff0c\u52a1\u5fc5\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c\u3002\u53ef\u4ee5\u4f7f\u7528\u5355\u5143\u6d4b\u8bd5\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u8c03\u8bd5\u5de5\u5177\u6765\u5e2e\u52a9\u5b9a\u4f4d\u548c\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p><strong>Python\u4e2d\u521b\u5efa\u65e0\u9650\u5faa\u73af\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528<code>while True<\/code>\u3001\u9012\u5f52\u51fd\u6570\u548c\u751f\u6210\u5668\u7b49\u3002<\/strong> \u6700\u5e38\u89c1\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>while True<\/code>\u5faa\u73af\uff0c\u4f46\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u5176\u4ed6\u65b9\u6cd5\u4e5f\u53ef\u80fd\u66f4\u5408\u9002\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u786e\u4fdd\u6709\u5408\u7406\u7684\u9000\u51fa\u6761\u4ef6\u3001\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3001\u4f18\u5316\u6027\u80fd\uff0c\u5e76\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5728Python\u4e2d\u7f16\u5199\u65e0\u9650\u5faa\u73af\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5e76\u4e86\u89e3\u4e86\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u548c\u5e94\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u77e5\u8bc6\u80fd\u591f\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u66f4\u597d\u5730\u4f7f\u7528\u65e0\u9650\u5faa\u73af\uff0c\u63d0\u5347\u7a0b\u5e8f\u7684\u7a33\u5b9a\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\u57fa\u672c\u7684\u65e0\u9650\u5faa\u73af\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u8bed\u53e5\u6765\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002\u53ea\u9700\u8bbe\u7f6e\u5faa\u73af\u6761\u4ef6\u4e3a<code>True<\/code>\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    print(&quot;\u8fd9\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&quot;)\n<\/code><\/pre>\n<p>\u53ea\u8981\u7a0b\u5e8f\u5728\u8fd0\u884c\uff0c\u8fd9\u4e2a\u5faa\u73af\u5c31\u4f1a\u4e0d\u65ad\u6267\u884c\uff0c\u76f4\u5230\u60a8\u624b\u52a8\u505c\u6b62\u5b83\uff0c\u6bd4\u5982\u901a\u8fc7\u952e\u76d8\u4e2d\u65ad\uff08Ctrl+C\uff09\u3002<\/p>\n<p><strong>\u65e0\u9650\u5faa\u73af\u4f1a\u5bfc\u81f4\u4ec0\u4e48\u95ee\u9898\uff1f\u5982\u4f55\u907f\u514d\uff1f<\/strong><br \/>\u65e0\u9650\u5faa\u73af\u5982\u679c\u4e0d\u52a0\u4ee5\u63a7\u5236\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5360\u7528\u5927\u91cf\u7684CPU\u8d44\u6e90\uff0c\u751a\u81f3\u4f7f\u8ba1\u7b97\u673a\u53d8\u5f97\u65e0\u54cd\u5e94\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u5faa\u73af\u5185\u90e8\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u6216\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6765\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u9000\u51fa\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    user_input = input(&quot;\u8f93\u5165&#39;\u9000\u51fa&#39;\u4ee5\u7ed3\u675f\u5faa\u73af\uff1a&quot;)\n    if user_input == &quot;\u9000\u51fa&quot;:\n        break\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u63a5\u6536\u5230\u7279\u5b9a\u8f93\u5165\u65f6\u5b89\u5168\u9000\u51fa\u3002<\/p>\n<p><strong>\u5728\u65e0\u9650\u5faa\u73af\u4e2d\u5982\u4f55\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u65e0\u9650\u5faa\u73af\u4e2d\u5904\u7406\u5f02\u5e38\u662f\u786e\u4fdd\u7a0b\u5e8f\u7a33\u5b9a\u8fd0\u884c\u7684\u91cd\u8981\u73af\u8282\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    try:\n        # \u53ef\u80fd\u4f1a\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n        x = int(input(&quot;\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff1a&quot;))\n        print(f&quot;\u60a8\u8f93\u5165\u7684\u6570\u5b57\u662f\uff1a{x}&quot;)\n    except ValueError:\n        print(&quot;\u65e0\u6548\u8f93\u5165\uff0c\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u9632\u6b62\u7a0b\u5e8f\u56e0\u8f93\u5165\u9519\u8bef\u800c\u5d29\u6e83\uff0c\u4f7f\u5176\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u65e0\u9650\u5faa\u73af\u4e2d\u9650\u5236\u6267\u884c\u6b21\u6570\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u5728\u65e0\u9650\u5faa\u73af\u4e2d\u9650\u5236\u6267\u884c\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u8ba1\u6570\u5668\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">count = 0\nwhile True:\n    print(&quot;\u8fd9\u662f\u7b2c&quot;, count + 1, &quot;\u6b21\u5faa\u73af&quot;)\n    count += 1\n    if count &gt;= 10:  # \u9650\u5236\u5faa\u73af\u6b21\u6570\u4e3a10\u6b21\n        break\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u7ed3\u675f\u5faa\u73af\uff0c\u907f\u514d\u4e86\u7a0b\u5e8f\u8fc7\u5ea6\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u7f16\u5199\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5305\u62ec\u4f7f\u7528while\u5faa\u73af\u3001for\u5faa\u73af\u548c\u9012\u5f52\u51fd\u6570\u3002 \u5e38\u89c1\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1118003,"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\/1117993"}],"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=1117993"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117993\/revisions"}],"predecessor-version":[{"id":1118005,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117993\/revisions\/1118005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118003"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}