{"id":1059249,"date":"2024-12-31T15:23:34","date_gmt":"2024-12-31T07:23:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059249.html"},"modified":"2024-12-31T15:23:36","modified_gmt":"2024-12-31T07:23:36","slug":"%e5%a6%82%e4%bd%95%e7%94%a8q%e6%88%96%e8%80%85p%e9%80%80%e5%87%ba%e7%a8%8b%e5%ba%8fpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059249.html","title":{"rendered":"\u5982\u4f55\u7528q\u6216\u8005p\u9000\u51fa\u7a0b\u5e8fpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/93c402c2-e7da-407d-878a-5e5016baa166.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528q\u6216\u8005p\u9000\u51fa\u7a0b\u5e8fpython\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6355\u6349\u7528\u6237\u8f93\u5165\u6765\u5b9e\u73b0\u9000\u51fa\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u6355\u6349\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\uff0c\u5e76\u6839\u636e\u8f93\u5165\u7684\u5185\u5bb9\u6765\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u7a0b\u5e8f\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>sys.exit()<\/code> \u51fd\u6570\u6765\u7ec8\u6b62\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 <code>input()<\/code> \u6355\u6349\u7528\u6237\u8f93\u5165\u3001\u6761\u4ef6\u5224\u65ad\u3001\u8c03\u7528 <code>sys.exit()<\/code> \u9000\u51fa\u7a0b\u5e8f\u3002<\/strong><br \/>\u5728\u7a0b\u5e8f\u4e2d\uff0c\u901a\u8fc7 <code>input()<\/code> \u51fd\u6570\u6355\u6349\u7528\u6237\u8f93\u5165\u7684\u5b57\u7b26\uff0c\u7136\u540e\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u68c0\u67e5\u8f93\u5165\u662f\u5426\u4e3a &#39;q&#39; \u6216 &#39;p&#39;\u3002\u5982\u679c\u8f93\u5165\u4e3a &#39;q&#39; \u6216 &#39;p&#39;\uff0c\u5219\u8c03\u7528 <code>sys.exit()<\/code> \u51fd\u6570\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;Enter &#39;q&#39; or &#39;p&#39; to exit, or any other key to continue: &quot;)<\/p>\n<p>    if user_input.lower() in (&#39;q&#39;, &#39;p&#39;):<\/p>\n<p>        print(&quot;Exiting the program.&quot;)<\/p>\n<p>        sys.exit()<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Continuing the program.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001 \u6355\u6349\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728Python\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>input()<\/code> \u51fd\u6570\u6355\u6349\u7528\u6237\u7684\u8f93\u5165\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u76f4\u5230\u7528\u6237\u8f93\u5165\u5185\u5bb9\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u4e3a\u6b62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;Please enter something: &quot;)<\/p>\n<p>print(&quot;You entered:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u7b49\u5f85\u7528\u6237\u8f93\u5165\u5185\u5bb9\uff0c\u5e76\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u8d4b\u503c\u7ed9 <code>user_input<\/code> \u53d8\u91cf\u3002\u7136\u540e\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001 \u6761\u4ef6\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u6355\u6349\u7528\u6237\u8f93\u5165\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u6765\u68c0\u67e5\u8f93\u5165\u7684\u5185\u5bb9\u3002\u6761\u4ef6\u5224\u65ad\u53ef\u4ee5\u4f7f\u7528 <code>if<\/code> \u8bed\u53e5\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_input = input(&quot;Please enter &#39;q&#39; or &#39;p&#39; to exit: &quot;)<\/p>\n<p>if user_input.lower() == &#39;q&#39; or user_input.lower() == &#39;p&#39;:<\/p>\n<p>    print(&quot;Exiting the program.&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;You entered:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u68c0\u67e5\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u662f\u5426\u4e3a &#39;q&#39; \u6216 &#39;p&#39;\u3002\u5982\u679c\u8f93\u5165\u7684\u5185\u5bb9\u662f &#39;q&#39; \u6216 &#39;p&#39;\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370 &quot;Exiting the program.&quot;\uff1b\u5426\u5219\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001 \u9000\u51fa\u7a0b\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sys.exit()<\/code> \u51fd\u6570\u9000\u51fa\u7a0b\u5e8f\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u7acb\u5373\u7ec8\u6b62\u7a0b\u5e8f\u7684\u6267\u884c\uff0c\u5e76\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u8fd4\u56de\u4e00\u4e2a\u9000\u51fa\u72b6\u6001\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>user_input = input(&quot;Please enter &#39;q&#39; or &#39;p&#39; to exit: &quot;)<\/p>\n<p>if user_input.lower() == &#39;q&#39; or user_input.lower() == &#39;p&#39;:<\/p>\n<p>    print(&quot;Exiting the program.&quot;)<\/p>\n<p>    sys.exit()<\/p>\n<p>else:<\/p>\n<p>    print(&quot;You entered:&quot;, user_input)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u662f &#39;q&#39; \u6216 &#39;p&#39;\uff0c\u7a0b\u5e8f\u4f1a\u6253\u5370 &quot;Exiting the program.&quot;\uff0c\u7136\u540e\u8c03\u7528 <code>sys.exit()<\/code> \u51fd\u6570\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001 \u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u7ed3\u5408\u4ee5\u4e0a\u5185\u5bb9\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u6355\u6349\u7528\u6237\u8f93\u5165\uff0c\u5e76\u6839\u636e\u8f93\u5165\u7684\u5185\u5bb9\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>while True:<\/p>\n<p>    user_input = input(&quot;Enter &#39;q&#39; or &#39;p&#39; to exit, or any other key to continue: &quot;)<\/p>\n<p>    if user_input.lower() in (&#39;q&#39;, &#39;p&#39;):<\/p>\n<p>        print(&quot;Exiting the program.&quot;)<\/p>\n<p>        sys.exit()<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Continuing the program.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u4f1a\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u6bcf\u6b21\u5faa\u73af\u90fd\u4f1a\u6355\u6349\u7528\u6237\u8f93\u5165\u3002\u5982\u679c\u7528\u6237\u8f93\u5165 &#39;q&#39; \u6216 &#39;p&#39;\uff0c\u7a0b\u5e8f\u4f1a\u9000\u51fa\uff1b\u5426\u5219\uff0c\u7a0b\u5e8f\u4f1a\u7ee7\u7eed\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001 \u63d0\u5347\u7528\u6237\u4f53\u9a8c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u52a0\u5165\u66f4\u591a\u7684\u63d0\u793a\u4fe1\u606f\uff0c\u5e76\u5904\u7406\u4e00\u4e9b\u53ef\u80fd\u7684\u9519\u8bef\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u63d0\u793a\u7528\u6237\u8f93\u5165\u65e0\u6548\uff0c\u5e76\u5141\u8bb8\u7528\u6237\u91cd\u65b0\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6539\u8fdb\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            user_input = input(&quot;Enter &#39;q&#39; or &#39;p&#39; to exit, or any other key to continue: &quot;)<\/p>\n<p>            if user_input.lower() in (&#39;q&#39;, &#39;p&#39;):<\/p>\n<p>                print(&quot;Exiting the program.&quot;)<\/p>\n<p>                sys.exit()<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;You entered:&quot;, user_input)<\/p>\n<p>        except KeyboardInterrupt:<\/p>\n<p>            print(&quot;\\nProgram interrupted by user. Exiting...&quot;)<\/p>\n<p>            sys.exit()<\/p>\n<p>        except Exception as e:<\/p>\n<p>            print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>            continue<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u6355\u6349\u4e86 <code>KeyboardInterrupt<\/code> \u5f02\u5e38\uff0c\u4ee5\u5904\u7406\u7528\u6237\u901a\u8fc7 Ctrl+C \u7ec4\u5408\u952e\u4e2d\u65ad\u7a0b\u5e8f\u7684\u60c5\u51b5\u3002\u6b64\u5916\uff0c\u7a0b\u5e8f\u8fd8\u6355\u6349\u4e86\u5176\u4ed6\u5f02\u5e38\uff0c\u5e76\u63d0\u793a\u7528\u6237\u53d1\u751f\u4e86\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001 \u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u6269\u5c55\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u6216\u8005\u6839\u636e\u7528\u6237\u8f93\u5165\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>import logging<\/p>\n<p>logging.basicConfig(filename=&#39;user_input.log&#39;, level=logging.INFO)<\/p>\n<p>def main():<\/p>\n<p>    while True:<\/p>\n<p>        try:<\/p>\n<p>            user_input = input(&quot;Enter &#39;q&#39; or &#39;p&#39; to exit, or any other key to continue: &quot;)<\/p>\n<p>            logging.info(f&quot;User input: {user_input}&quot;)<\/p>\n<p>            if user_input.lower() in (&#39;q&#39;, &#39;p&#39;):<\/p>\n<p>                print(&quot;Exiting the program.&quot;)<\/p>\n<p>                sys.exit()<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;You entered:&quot;, user_input)<\/p>\n<p>        except KeyboardInterrupt:<\/p>\n<p>            print(&quot;\\nProgram interrupted by user. Exiting...&quot;)<\/p>\n<p>            sys.exit()<\/p>\n<p>        except Exception as e:<\/p>\n<p>            logging.error(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>            print(f&quot;An error occurred: {e}&quot;)<\/p>\n<p>            continue<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u7a0b\u5e8f\u5c06\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6 <code>user_input.log<\/code> \u4e2d\uff0c\u5e76\u8bb0\u5f55\u4e86\u53d1\u751f\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001 \u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u5728Python\u7a0b\u5e8f\u4e2d\u6355\u6349\u7528\u6237\u8f93\u5165\uff0c\u5e76\u6839\u636e\u8f93\u5165\u7684\u5185\u5bb9\u51b3\u5b9a\u662f\u5426\u9000\u51fa\u7a0b\u5e8f\u3002\u6211\u4eec\u8fd8\u4e86\u89e3\u4e86\u5982\u4f55\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u6269\u5c55\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f16\u5199\u66f4\u52a0\u5065\u58ee\u548c\u7528\u6237\u53cb\u597d\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u7a0b\u5e8f\u7684\u903b\u8f91\u548c\u529f\u80fd\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06\u9000\u51fa\u7a0b\u5e8f\u7684\u6761\u4ef6\u8bbe\u7f6e\u4e3a\u5176\u4ed6\u5b57\u7b26\u6216\u7ec4\u5408\u952e\uff0c\u6216\u8005\u6839\u636e\u7528\u6237\u8f93\u5165\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u9ad8\u6548\u548c\u5b9e\u7528\u7684Python\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6309\u952e\u9000\u51fa\u7a0b\u5e8f\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u76d1\u542c\u7528\u6237\u8f93\u5165\u6765\u5b9e\u73b0\u6309\u952e\u9000\u51fa\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\uff0c\u7b49\u5f85\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u5b57\u7b26\uff08\u5982\u2018q\u2019\u6216\u2018p\u2019\uff09\u6765\u9000\u51fa\u7a0b\u5e8f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">while True:\n    command = input(&quot;\u8f93\u5165 &#39;q&#39; \u6216 &#39;p&#39; \u9000\u51fa\u7a0b\u5e8f\uff1a&quot;)\n    if command in [&#39;q&#39;, &#39;p&#39;]:\n        print(&quot;\u7a0b\u5e8f\u5df2\u9000\u51fa&quot;)\n        break\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u6765\u5b89\u5168\u9000\u51fa\u7a0b\u5e8f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u6765\u5b89\u5168\u5730\u9000\u51fa\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u7279\u5b9a\u7684\u8f93\u5165\u9519\u8bef\u6216\u952e\u76d8\u4e2d\u65ad\u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u5347\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u907f\u514d\u56e0\u4e0d\u5f53\u8f93\u5165\u5bfc\u81f4\u7684\u7a0b\u5e8f\u5d29\u6e83\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">try:\n    while True:\n        command = input(&quot;\u8f93\u5165 &#39;q&#39; \u6216 &#39;p&#39; \u9000\u51fa\u7a0b\u5e8f\uff1a&quot;)\n        if command in [&#39;q&#39;, &#39;p&#39;]:\n            print(&quot;\u7a0b\u5e8f\u5df2\u9000\u51fa&quot;)\n            break\nexcept KeyboardInterrupt:\n    print(&quot;\\n\u7a0b\u5e8f\u88ab\u7528\u6237\u4e2d\u65ad&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u8ba9\u7528\u6237\u80fd\u591f\u901a\u8fc7Ctrl+C\u6765\u5b89\u5168\u9000\u51fa\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u6ca1\u6709\u66f4\u590d\u6742\u7684\u9000\u51fa\u7a0b\u5e8f\u7684\u65b9\u6cd5\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u7f16\u7a0b\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u9000\u51fa\u673a\u5236\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u7ebf\u7a0b\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u540c\u65f6\u76d1\u542c\u7279\u5b9a\u7684\u8f93\u5165\u6765\u51b3\u5b9a\u4f55\u65f6\u9000\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import threading\nimport time\n\ndef run_program():\n    while True:\n        print(&quot;\u7a0b\u5e8f\u8fd0\u884c\u4e2d...&quot;)\n        time.sleep(1)\n\ndef listen_for_exit():\n    while True:\n        command = input()\n        if command in [&#39;q&#39;, &#39;p&#39;]:\n            print(&quot;\u7a0b\u5e8f\u5df2\u9000\u51fa&quot;)\n            break\n\nthreading.Thread(target=run_program).start()\nlisten_for_exit()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u7a0b\u5e8f\u5728\u8fd0\u884c\u7684\u540c\u65f6\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\uff0c\u9002\u5408\u9700\u8981\u6301\u7eed\u8fd0\u884c\u7684\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6355\u6349\u7528\u6237\u8f93\u5165\u6765\u5b9e\u73b0\u9000\u51fa\u7a0b\u5e8f\u7684\u529f\u80fd\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528 input() \u51fd\u6570\u6355\u6349\u7528\u6237\u8f93 [&hellip;]","protected":false},"author":3,"featured_media":1059255,"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\/1059249"}],"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=1059249"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059249\/revisions"}],"predecessor-version":[{"id":1059259,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059249\/revisions\/1059259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059255"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}