{"id":178771,"date":"2024-05-08T19:57:24","date_gmt":"2024-05-08T11:57:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/178771.html"},"modified":"2024-05-08T19:57:29","modified_gmt":"2024-05-08T11:57:29","slug":"%e6%97%a0%e6%b3%95%e5%88%a4%e6%96%adpython%e4%bb%80%e4%b9%88%e6%83%85%e5%86%b5%e4%b8%8b%e7%94%a8while-true","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/178771.html","title":{"rendered":"\u65e0\u6cd5\u5224\u65adpython\u4ec0\u4e48\u60c5\u51b5\u4e0b\u7528while  true"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063347\/65001619-c26a-4915-a9d1-216a9bb170e9.webp\" alt=\"\u65e0\u6cd5\u5224\u65adpython\u4ec0\u4e48\u60c5\u51b5\u4e0b\u7528while  true\" \/><\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5e76\u4e14\u5faa\u73af\u4e2d\u5305\u542b\u4e86\u5224\u65ad\u6216\u4e2d\u65ad\u6761\u4ef6\uff08\u901a\u5e38\u901a\u8fc7<code>break<\/code>\u8bed\u53e5\u5b9e\u73b0\uff09\u6765\u9000\u51fa\u5faa\u73af\u65f6\uff0c\u4f7f\u7528<code>while True<\/code>\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u505a\u6cd5\u3002<strong>\u5728\u4ee5\u4e0b\u51e0\u79cd\u573a\u666f\u4e2d\uff0c\u4f7f\u7528<code>while True<\/code>\u662f\u975e\u5e38\u5408\u9002\u7684<\/strong>\uff1a\u8fdb\u884c\u6301\u7eed\u7684\u7528\u6237\u4ea4\u4e92\u3001\u76d1\u542c\u7cfb\u7edf\u4e8b\u4ef6\u76f4\u5230\u663e\u5f0f\u4e2d\u65ad\u3001\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u670d\u52a1\u6216\u5b88\u62a4\u8fdb\u7a0b\u3001\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u4e3b\u5faa\u73af\u3001\u7b49\u5f85\u5916\u90e8\u8d44\u6e90\u6216\u8f93\u5165\u3002\u5728\u4f7f\u7528<code>while True<\/code>\u7684\u60c5\u51b5\u4e0b\uff0c<strong>\u53ef\u4ee5\u5728\u5faa\u73af\u5185\u90e8\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u548c<code>break<\/code>\u8bed\u53e5\u5b89\u5168\u9000\u51fa\u5faa\u73af\uff0c\u4ece\u800c\u907f\u514d\u65e0\u5c3d\u7684\u6267\u884c<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001<strong>\u5b9e\u73b0\u6301\u7eed\u4ea4\u4e92\u7684\u7528\u6237\u754c\u9762<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u9700\u6c42\u5b9e\u65f6\u7528\u6237\u8f93\u5165\u7684\u7a0b\u5e8f\u65f6\uff0c\u901a\u5e38\u9700\u8981\u4e00\u4e2a\u4e3b\u5faa\u73af\u6765\u6301\u7eed\u5730\u63a5\u53d7\u548c\u5904\u7406\u7528\u6237\u7684\u547d\u4ee4\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<code>while True<\/code>\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e00\u76f4\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff0c\u76f4\u81f3\u7528\u6237\u660e\u786e\u6307\u793a\u7a0b\u5e8f\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u884c\u754c\u9762\u7a0b\u5e8f\u53ef\u80fd\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = input(&#039;\u8bf7\u8f93\u5165\u547d\u4ee4: &#039;)<\/p>\n<p>    if user_input == &#039;exit&#039;:<\/p>\n<p>        print(&#039;\u7a0b\u5e8f\u9000\u51fa\u3002&#039;)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        process_command(user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001<strong>\u76d1\u542c\u7cfb\u7edf\u4e8b\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u7a0b\u5e8f\u6301\u7eed\u76d1\u542c\u5e76\u54cd\u5e94\u7cfb\u7edf\u4e8b\u4ef6\u7684\u573a\u5408\uff08\u5982\u670d\u52a1\u5668\u8f6f\u4ef6\u6216\u786c\u4ef6\u4e8b\u4ef6\u76d1\u542c\u5668\uff09\uff0c<code>while True<\/code>\u53ef\u4ee5\u7528\u6765\u8868\u793a\u65e0\u9650\u76d1\u542c\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u670d\u52a1\u5668\u76d1\u542c\u5ba2\u6237\u7aef\u8bf7\u6c42\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    event = listen_for_event()<\/p>\n<p>    if event.is_shutdown_signal():<\/p>\n<p>        print(&#039;\u670d\u52a1\u5668\u6b63\u5728\u5173\u95ed\u3002&#039;)<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        handle_event(event)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001<strong>\u957f\u65f6\u95f4\u8fd0\u884c\u670d\u52a1\u6216\u5b88\u62a4\u8fdb\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u670d\u52a1\u6216\u5b88\u62a4\u8fdb\u7a0b\uff0c\u8fd9\u4e9b\u8fdb\u7a0b\u901a\u5e38\u9700\u8981\u5728\u7cfb\u7edf\u542f\u52a8\u540e\u6c38\u4e45\u8fd0\u884c\uff0c\u5bf9\u6b64\u53ef\u4ee5\u4f7f\u7528<code>while True<\/code>\u7ed3\u6784\u6765\u4fdd\u6301\u5176\u6d3b\u8dc3\u72b6\u6001\uff0c\u5e76\u901a\u8fc7\u7cfb\u7edf\u670d\u52a1\u7ba1\u7406\u6216\u4f7f\u7528\u5185\u90e8\u903b\u8f91\u6765\u6267\u884c\u5fc5\u8981\u7684\u505c\u6b62\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u90ae\u4ef6\u670d\u52a1\u5668\u53ef\u80fd\u4f1a\u6709\u5982\u4e0b\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l = check_for_new_email()<\/p>\n<p>    if email:<\/p>\n<p>        process_email(email)<\/p>\n<p>    perform_maintenance_tasks()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001<strong>\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u4e3b\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>\u6e38\u620f\u7684\u5f00\u53d1\u4e2d\uff0c<code>while True<\/code>\u5e38\u88ab\u7528\u6765\u5b9e\u73b0\u6e38\u620f\u7684\u4e3b\u5faa\u73af\uff0c\u5373\u201c\u6e38\u620f\u5faa\u73af\u201d\u3002\u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u66f4\u65b0\u6e38\u620f\u72b6\u6001\uff0c\u5e76\u91cd\u65b0\u6e32\u67d3\u6e38\u620f\u753b\u9762\u3002\u53ea\u6709\u5728\u7528\u6237\u9000\u51fa\u6e38\u620f\u65f6\u5faa\u73af\u624d\u4f1a\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><p>\u6e38\u620f\u5faa\u73af\u53ef\u80fd\u770b\u8d77\u6765\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    user_input = get_user_input()<\/p>\n<p>    if user_input == &#039;QUIT&#039;:<\/p>\n<p>        print(&#039;\u6e38\u620f\u9000\u51fa\u3002&#039;)<\/p>\n<p>        break<\/p>\n<p>    update_game_state(user_input)<\/p>\n<p>    render_game_screen()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001<strong>\u7b49\u5f85\u5916\u90e8\u8d44\u6e90\u6216\u8f93\u5165<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u7a0b\u5e8f\u663e\u5f0f\u7b49\u5f85\u5916\u90e8\u4e8b\u4ef6\u6216\u8d44\u6e90\u51c6\u5907\u5c31\u7eea\u7684\u60c5\u51b5\u4e0b\uff0c<code>while True<\/code>\u6784\u6210\u4e86\u4e00\u4e2a\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u8f6e\u8be2\u72b6\u6001\uff0c\u5e76\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u7ee7\u7eed\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u7a0b\u5e8f\u9700\u8981\u7b49\u5f85\u6587\u4ef6\u53ef\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    if is_file_ready(filename):<\/p>\n<p>        print(&#039;\u5904\u7406\u6587\u4ef6\u3002&#039;)<\/p>\n<p>        process_file(filename)<\/p>\n<p>        break<\/p>\n<p>    sleep(5)  # \u4f11\u7720\u4e00\u6bb5\u65f6\u95f4\u518d\u6b21\u68c0\u67e5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>while True<\/code>\u65f6\u8fd8\u9700\u8981\u6ce8\u610f\u786e\u4fdd\u5faa\u73af\u5185\u6709\u5408\u7406\u7684\u903b\u8f91\uff0c\u7279\u522b\u662f\u9000\u51fa\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u9020\u6210\u7a0b\u5e8f\u7684\u65e0\u9650\u5faa\u73af\u548c\u8d44\u6e90\u7684\u6d6a\u8d39\u3002\u540c\u65f6\u5728\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528<code>while True<\/code>\u8fd8\u9700\u8981\u7279\u522b\u6ce8\u610f\u540c\u6b65\u548c\u5e76\u53d1\u95ee\u9898\uff0c\u4ee5\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528\u65e0\u9650\u5faa\u73af\uff08while True\uff09\u6765\u7f16\u5199Python\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>\u65e0\u9650\u5faa\u73af\u5728Python\u4e2d\u7ecf\u5e38\u88ab\u7528\u4e8e\u9700\u8981\u4e0d\u65ad\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\u76f4\u5230\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\u3002\u4e00\u4e9b\u5e38\u89c1\u7684\u60c5\u51b5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5b9e\u65f6\u6570\u636e\u76d1\u63a7\uff1a\u5f53\u9700\u8981\u4e0d\u65ad\u76d1\u63a7\u5b9e\u65f6\u6570\u636e\u5e76\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8fdb\u884c\u54cd\u5e94\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3002<\/li>\n<li>\u4e8b\u4ef6\u9a71\u52a8\u7a0b\u5e8f\uff1a\u5904\u7406\u4e8b\u4ef6\u7684\u7a0b\u5e8f\u4e2d\uff0c\u65e0\u9650\u5faa\u73af\u53ef\u4ee5\u4e0d\u65ad\u68c0\u67e5\u4e8b\u4ef6\u961f\u5217\u5e76\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u591a\u7ebf\u7a0b\u63a7\u5236\uff1a\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u53ef\u4ee5\u5b9e\u65f6\u68c0\u67e5\u7ebf\u7a0b\u72b6\u6001\u5e76\u8fdb\u884c\u5fc5\u8981\u7684\u8c03\u5ea6\u3002<\/li>\n<li>\u5f02\u6b65\u4efb\u52a1\u5904\u7406\uff1a\u5f53\u9700\u8981\u4e0d\u65ad\u68c0\u67e5\u5f02\u6b65\u4efb\u52a1\u7684\u5b8c\u6210\u72b6\u6001\u5e76\u6267\u884c\u540e\u7eed\u64cd\u4f5c\u65f6\uff0c\u65e0\u9650\u5faa\u73af\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<p><strong>2. \u9664\u4e86while True\uff0c\u8fd8\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u65e0\u9650\u5faa\u73af\u5199\u6cd5\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528<code>while True<\/code>\u4ee5\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u6765\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u3002\u4e0b\u9762\u5217\u4e3e\u4e86\u51e0\u79cd\u5e38\u89c1\u7684\u5199\u6cd5\uff1a<\/p>\n<ul>\n<li><code>while 1<\/code>\uff1a\u5c06\u6570\u5b571\u4f5c\u4e3awhile\u7684\u6761\u4ef6\uff0c\u56e0\u4e3a1\u5728\u5e03\u5c14\u8868\u8fbe\u5f0f\u4e2d\u88ab\u89c6\u4e3aTrue\uff0c\u6240\u4ee5\u4e5f\u662f\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u7684\u5199\u6cd5\u3002<\/li>\n<li><code>while not False<\/code>\uff1a\u4f7f\u7528\u903b\u8f91\u975e\u64cd\u4f5c\u7b26\u6765\u5f62\u6210\u4e00\u4e2a\u6c38\u8fdc\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u3002<\/li>\n<li><code>while 1==1<\/code>\u6216<code>while 2&gt;1<\/code>\uff1a\u901a\u8fc7\u4f7f\u7528\u59cb\u7ec8\u4e3a\u771f\u7684\u903b\u8f91\u8868\u8fbe\u5f0f\uff0c\u59821\u7b49\u4e8e1\u62162\u5927\u4e8e1\uff0c\u6765\u5b9e\u73b0\u65e0\u9650\u5faa\u73af\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u5728\u65e0\u9650\u5faa\u73af\u4e2d\u5b89\u5168\u5730\u4e2d\u65ad\u6216\u8df3\u51fa\u5faa\u73af\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u65f6\uff0c\u5fc5\u987b\u8c28\u614e\u5904\u7406\u5faa\u73af\u4e2d\u7684\u4e2d\u65ad\u6216\u8df3\u51fa\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u9677\u5165\u6b7b\u5faa\u73af\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5b89\u5168\u4e2d\u65ad\u6216\u8df3\u51fa\u5faa\u73af\u7684\u5e38\u7528\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528break\u8bed\u53e5\uff1a\u5728\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528break\u8bed\u53e5\u6765\u7acb\u5373\u8df3\u51fa\u5faa\u73af\u3002<\/li>\n<li>\u8bbe\u7f6e\u5faa\u73af\u8ba1\u6570\u5668\u548c\u6761\u4ef6\uff1a\u53ef\u4ee5\u4f7f\u7528\u8ba1\u6570\u5668\u6216\u8005\u5e03\u5c14\u6761\u4ef6\u6765\u9650\u5236\u5faa\u73af\u6b21\u6570\u6216\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u9000\u51fa\u5faa\u73af\u3002<\/li>\n<li>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff1a\u5728\u5faa\u73af\u4f53\u4e2d\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\uff0c\u5e76\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u8df3\u51fa\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u7279\u5b9a\u7684\u95ee\u9898\u65f6\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<p>\u603b\u4e4b\uff0c\u65e0\u9650\u5faa\u73af\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u9700\u8981\u5c0f\u5fc3\u9677\u5165\u6b7b\u5faa\u73af\u7684\u60c5\u51b5\uff0c\u5e76\u8003\u8651\u5982\u4f55\u5b89\u5168\u5730\u4e2d\u65ad\u6216\u8df3\u51fa\u5faa\u73af\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f53\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u5e76\u4e14\u5faa\u73af\u4e2d\u5305\u542b\u4e86\u5224\u65ad\u6216\u4e2d\u65ad\u6761\u4ef6\uff08\u901a\u5e38\u901a\u8fc7break\u8bed\u53e5\u5b9e\u73b0\uff09\u6765\u9000\u51fa\u5faa\u73af\u65f6\uff0c\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":178779,"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\/178771"}],"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=178771"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/178771\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/178779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=178771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=178771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=178771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}