{"id":961229,"date":"2024-12-27T03:56:51","date_gmt":"2024-12-26T19:56:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/961229.html"},"modified":"2024-12-27T03:56:54","modified_gmt":"2024-12-26T19:56:54","slug":"python%e5%a6%82%e4%bd%95%e6%8c%81%e7%bb%ad%e8%be%93%e5%87%bahello","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/961229.html","title":{"rendered":"python\u5982\u4f55\u6301\u7eed\u8f93\u51faHello"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103528\/37b7a7b0-02b1-4485-ba64-448e187a35f3.webp\" alt=\"python\u5982\u4f55\u6301\u7eed\u8f93\u51faHello\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9Python\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u6216\u5229\u7528\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528while\u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u61c2\u3001\u6613\u4e8e\u5b9e\u73b0\u3002\u901a\u8fc7while\u5faa\u73af\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u4e0d\u65ad\u6267\u884c\u67d0\u6bb5\u4ee3\u7801\uff0c\u76f4\u5230\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u624d\u4f1a\u9000\u51fa\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00while\u5faa\u73af\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cwhile\u5faa\u73af\u662f\u4e00\u4e2a\u5f88\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\u6301\u7eed\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u6c38\u8fdc\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u6bd4\u5982True\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    print(&quot;Hello&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u65e0\u9650\u5faa\u73af\u8f93\u51fa\u201cHello\u201d\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u52a0\u5165\u6761\u4ef6\u5224\u65ad\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u65b9\u5f0f\u6765\u63a7\u5236\u5faa\u73af\u7684\u7ec8\u6b62\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u5faa\u73af\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8Python\u4e2d\u5176\u4ed6\u51e0\u79cd\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528WHILE\u5faa\u73af<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u5b9e\u73b0<\/strong><\/p>\n<\/p>\n<p><p>while\u5faa\u73af\u662f\u4e00\u79cd\u6700\u76f4\u63a5\u3001\u7b80\u5355\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u53ea\u8981\u6761\u4ef6\u4e3a\u771f\uff0c\u5c31\u4f1a\u4e00\u76f4\u6267\u884c\u4e0b\u53bb\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u6ca1\u6709\u5408\u9002\u7684\u9000\u51fa\u6761\u4ef6\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u5faa\u73af\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63a7\u5236\u5faa\u73af\u7ec8\u6b62<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u52a0\u5165\u4e00\u4e9b\u6761\u4ef6\u6765\u63a7\u5236\u5faa\u73af\u7684\u7ec8\u6b62\u3002\u53ef\u4ee5\u4f7f\u7528\u8ba1\u6570\u5668\u3001\u7528\u6237\u8f93\u5165\u6216\u8005\u65f6\u95f4\u9650\u5236\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 10:<\/p>\n<p>    print(&quot;Hello&quot;)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8ba1\u6570\u5668<code>count<\/code>\u6765\u63a7\u5236\u5faa\u73af\uff0c\u53ea\u6253\u537010\u6b21\u201cHello\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9012\u5f52\u51fd\u6570\u7684\u6982\u5ff5<\/strong><\/p>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u7684\u65b9\u6cd5\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u800c\u6cbb\u4e4b\u7684\u95ee\u9898\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9012\u5f52\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9\u51fd\u6570\u4e00\u76f4\u8c03\u7528\u81ea\u8eab\uff0c\u4ece\u800c\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9012\u5f52\u9700\u8981\u4e00\u4e2a\u660e\u786e\u7684\u9000\u51fa\u6761\u4ef6\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_hello():<\/p>\n<p>    print(&quot;Hello&quot;)<\/p>\n<p>    print_hello()<\/p>\n<p>print_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684\u4ee3\u7801\u4f1a\u5bfc\u81f4\u65e0\u9650\u9012\u5f52\uff0c\u6700\u7ec8\u5f15\u53d1\u6808\u6ea2\u51fa\u9519\u8bef\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u52a0\u5165\u6761\u4ef6\u5224\u65ad\u6765\u907f\u514d\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u7f16\u7a0b<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u591a\u7ebf\u7a0b\u7684\u6982\u5ff5<\/strong><\/p>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u662f\u4e00\u79cd\u5728\u7a0b\u5e8f\u4e2d\u540c\u65f6\u8fd0\u884c\u591a\u4e2a\u7ebf\u7a0b\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u7279\u522b\u662f\u5f53\u7a0b\u5e8f\u4e2d\u6709\u9700\u8981\u5e76\u53d1\u6267\u884c\u7684\u4efb\u52a1\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>threading<\/code>\u6a21\u5757\u6765\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def print_hello():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;Hello&quot;)<\/p>\n<p>        time.sleep(1)  # \u5ef6\u8fdf1\u79d2\u4ee5\u907f\u514d\u8fc7\u5feb\u8f93\u51fa<\/p>\n<p>thread = threading.Thread(target=print_hello)<\/p>\n<p>thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u6765\u6267\u884c<code>print_hello<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u3002\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6700\u597d\u52a0\u5165\u4e00\u4e9b\u5ef6\u8fdf\uff0c\u4ee5\u51cf\u5c11CPU\u7684\u5360\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u907f\u514d\u6b7b\u5faa\u73af<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u907f\u514d\u7a0b\u5e8f\u8fdb\u5165\u6b7b\u5faa\u73af\u72b6\u6001\uff0c\u8fd9\u4e0d\u4ec5\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u54cd\u5e94\uff0c\u8fd8\u53ef\u80fd\u6d88\u8017\u5927\u91cf\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9002\u5f53\u7684\u9000\u51fa\u6761\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528while\u5faa\u73af\u3001\u9012\u5f52\u8fd8\u662f\u591a\u7ebf\u7a0b\uff0c\u90fd\u5e94\u8be5\u8bbe\u8ba1\u5408\u7406\u7684\u9000\u51fa\u6761\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u8f93\u5165\u3001\u4e2d\u65ad\u4fe1\u53f7\u6216\u5176\u4ed6\u89e6\u53d1\u6761\u4ef6\u6765\u7ec8\u6b62\u5faa\u73af\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/strong><\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u8d44\u6e90\u7ba1\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u95ee\u9898\u3002\u9700\u8981\u786e\u4fdd\u7ebf\u7a0b\u7684\u6b63\u786e\u7ec8\u6b62\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002\u6b64\u5916\uff0c\u5408\u7406\u8bbe\u7f6e\u7ebf\u7a0b\u4e4b\u95f4\u7684\u5171\u4eab\u6570\u636e\u548c\u9501\uff0c\u53ef\u4ee5\u907f\u514d\u7ade\u6001\u6761\u4ef6\u548c\u6b7b\u9501\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u672f\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u5b9e\u73b0\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5faa\u73af\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6216<code>for<\/code>\u5faa\u73af\u6765\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">while True:\n    print(&quot;Hello&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u4e0d\u65ad\u6253\u5370\u201cHello\u201d\uff0c\u76f4\u5230\u624b\u52a8\u505c\u6b62\u7a0b\u5e8f\u3002\u5982\u679c\u60f3\u8981\u5728\u6bcf\u6b21\u8f93\u51fa\u4e4b\u95f4\u6dfb\u52a0\u5ef6\u8fdf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u9650\u5236\u201cHello\u201d\u8f93\u51fa\u7684\u6b21\u6570\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u8f93\u51fa\u201cHello\u201d\u4e00\u5b9a\u7684\u6b21\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u201cHello\u201d\u8f93\u51fa\u4e94\u6b21\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(5):\n    print(&quot;Hello&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u8f93\u51fa\u7684\u6b21\u6570\u662f\u53ef\u63a7\u7684\uff0c\u9002\u5408\u4e8e\u9700\u8981\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u8f93\u51fa\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u6765\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\u7684\u529f\u80fd\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u590d\u7528\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def print_hello():\n    while True:\n        print(&quot;Hello&quot;)\n\nprint_hello()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u8f93\u51fa\u903b\u8f91\u5c01\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u65b9\u4fbf\u5728\u5176\u4ed6\u5730\u65b9\u8c03\u7528\u6216\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9Python\u6301\u7eed\u8f93\u51fa\u201cHello\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528while\u5faa\u73af\u3001\u9012\u5f52\u51fd\u6570\u6216\u5229\u7528\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u5176\u4e2d\uff0c\u4f7f\u7528while [&hellip;]","protected":false},"author":3,"featured_media":961235,"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\/961229"}],"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=961229"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961229\/revisions"}],"predecessor-version":[{"id":961241,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961229\/revisions\/961241"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/961235"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=961229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=961229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=961229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}