{"id":974358,"date":"2024-12-27T06:04:31","date_gmt":"2024-12-26T22:04:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974358.html"},"modified":"2024-12-27T06:04:33","modified_gmt":"2024-12-26T22:04:33","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8while","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974358.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528while"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200110\/4a1390a4-1da2-4023-951d-54344cf4d8b1.webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528while\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u7684\u65b9\u6cd5\u662f\uff1a\u5b9a\u4e49\u5faa\u73af\u6761\u4ef6\u3001\u4f7f\u7528<code>while<\/code>\u8bed\u53e5\u3001\u7f16\u5199\u5faa\u73af\u4f53\u3001\u63a7\u5236\u5faa\u73af\u7ed3\u675f\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u5b9a\u4e49\u5faa\u73af\u6761\u4ef6<\/strong>\u662f\u6700\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u5b83\u51b3\u5b9a\u4e86\u5faa\u73af\u7684\u6267\u884c\u6b21\u6570\u3002<strong>\u5177\u4f53\u6765\u8bf4\uff0cwhile\u5faa\u73af\u7528\u4e8e\u5728\u7279\u5b9a\u6761\u4ef6\u4e3a\u771f\u65f6\u53cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\uff1b\u5982\u679c\u6761\u4ef6\u59cb\u7ec8\u4e3a\u771f\uff0c\u5faa\u73af\u5c06\u65e0\u9650\u6267\u884c\uff0c\u6240\u4ee5\u8981\u786e\u4fdd\u5728\u67d0\u4e2a\u65f6\u523b\u6761\u4ef6\u4e3a\u5047\u4ee5\u7ed3\u675f\u5faa\u73af<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b9a\u4e49\u5faa\u73af\u6761\u4ef6<\/p>\n<\/p>\n<p><p>\u5b9a\u4e49\u5faa\u73af\u6761\u4ef6\u662f\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u7684\u7b2c\u4e00\u6b65\u3002\u5faa\u73af\u6761\u4ef6\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5b83\u51b3\u5b9a\u4e86\u5faa\u73af\u662f\u5426\u7ee7\u7eed\u6267\u884c\u3002\u901a\u5e38\uff0c\u8fd9\u4e2a\u6761\u4ef6\u4f1a\u5728\u6bcf\u6b21\u5faa\u73af\u5f00\u59cb\u65f6\u8fdb\u884c\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u5faa\u73af\u6761\u4ef6\u65f6\uff0c\u8981\u786e\u4fdd\u5176\u80fd\u5728\u67d0\u4e2a\u65f6\u523b\u53d8\u4e3a\u5047\uff0c\u4ece\u800c\u9000\u51fa\u5faa\u73af\u3002\u5426\u5219\uff0c\u7a0b\u5e8f\u5c06\u8fdb\u5165\u65e0\u9650\u5faa\u73af\uff0c\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002\u901a\u5e38\uff0c\u8fd9\u9700\u8981\u5728\u5faa\u73af\u4f53\u5185\u6539\u53d8\u53d8\u91cf\u7684\u503c\uff0c\u4ee5\u786e\u4fdd\u6761\u4ef6\u6700\u7ec8\u4e3a\u5047\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(&quot;Count is:&quot;, count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code>\u4ece0\u5f00\u59cb\uff0c\u6bcf\u6b21\u5faa\u73af\u540e\u589e\u52a01\uff0c\u5f53<code>count<\/code>\u7b49\u4e8e5\u65f6\uff0c\u6761\u4ef6<code>count &lt; 5<\/code>\u53d8\u4e3a\u5047\uff0c\u5faa\u73af\u7ed3\u675f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>while<\/code>\u8bed\u53e5<\/p>\n<\/p>\n<p><p><code>while<\/code>\u8bed\u53e5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u5faa\u73af\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while condition:<\/p>\n<p>    # Code to execute<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>condition<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u5728<code>condition<\/code>\u4e3a\u771f\u65f6\u6267\u884c\u3002\u91cd\u8981\u7684\u662f\u8981\u786e\u4fdd<code>condition<\/code>\u6700\u7ec8\u4f1a\u53d8\u4e3a\u5047\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u5728<code>while<\/code>\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u7acb\u5373\u9000\u51fa\u5faa\u73af\uff0c\u6216\u4f7f\u7528<code>continue<\/code>\u8bed\u53e5\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u7684\u5269\u4f59\u4ee3\u7801\u5e76\u5f00\u59cb\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u7f16\u5199\u5faa\u73af\u4f53<\/p>\n<\/p>\n<p><p>\u5faa\u73af\u4f53\u662f<code>while<\/code>\u5faa\u73af\u4e2d\u9700\u8981\u53cd\u590d\u6267\u884c\u7684\u4ee3\u7801\u5757\u3002\u5728\u5faa\u73af\u4f53\u4e2d\uff0c\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u8f93\u5165\u8f93\u51fa\u3001\u8ba1\u7b97\u3001\u6761\u4ef6\u5224\u65ad\u7b49\u3002\u5faa\u73af\u4f53\u4e2d\u7684\u4ee3\u7801\u901a\u5e38\u4f1a\u6539\u53d8\u67d0\u4e2a\u53d8\u91cf\u7684\u72b6\u6001\uff0c\u4ee5\u5f71\u54cd\u5faa\u73af\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5faa\u73af\u4f53\u65f6\uff0c\u8981\u786e\u4fdd\u4ee3\u7801\u7684\u903b\u8f91\u6b63\u786e\u4e14\u9ad8\u6548\u3002\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5efa\u8bae\u5728\u5faa\u73af\u4f53\u4e2d\u52a0\u5165\u6ce8\u91ca\uff0c\u89e3\u91ca\u4ee3\u7801\u7684\u529f\u80fd\u548c\u610f\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u63a7\u5236\u5faa\u73af\u7ed3\u675f<\/p>\n<\/p>\n<p><p>\u63a7\u5236\u5faa\u73af\u7ed3\u675f\u662f\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u65f6\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u4e3a\u4e86\u9632\u6b62\u7a0b\u5e8f\u9677\u5165\u65e0\u9650\u5faa\u73af\uff0c\u5fc5\u987b\u5728\u5faa\u73af\u4f53\u4e2d\u505a\u51fa\u9002\u5f53\u7684\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u6700\u7ec8\u53d8\u4e3a\u5047\u3002<\/p>\n<\/p>\n<p><p>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4fee\u6539\u5faa\u73af\u6761\u4ef6\u7684\u53d8\u91cf\uff0c\u4f7f\u5176\u5728\u67d0\u4e2a\u65f6\u523b\u4e0d\u6ee1\u8db3\u6761\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u7acb\u5373\u9000\u51fa\u5faa\u73af\u3002<\/li>\n<li>\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6355\u83b7\u53ef\u80fd\u5bfc\u81f4\u5faa\u73af\u65e0\u6cd5\u7ed3\u675f\u7684\u5f02\u5e38\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5408\u7406\u5730\u63a7\u5236\u5faa\u73af\u7ed3\u675f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001<code>while<\/code>\u5faa\u73af\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u5728Python\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5c24\u5176\u9002\u5408\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u5f53\u9700\u8981\u53cd\u590d\u63d0\u793a\u7528\u6237\u8f93\u5165\u76f4\u81f3\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u6709\u6548\u7684\u6574\u6570\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    try:<\/p>\n<p>        number = int(input(&quot;Enter a valid integer: &quot;))<\/p>\n<p>        break<\/p>\n<p>    except ValueError:<\/p>\n<p>        print(&quot;Invalid input. Please try ag<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\n<p><strong>\u6570\u636e\u5904\u7406<\/strong>\uff1a\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u7279\u522b\u662f\u5f53\u6570\u636e\u91cf\u672a\u77e5\u6216\u4e0d\u786e\u5b9a\u65f6\uff0c<code>while<\/code>\u5faa\u73af\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u9010\u884c\u8bfb\u53d6\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5faa\u73af\u8ba1\u6570<\/strong>\uff1a\u5f53\u9700\u8981\u6839\u636e\u67d0\u4e2a\u6761\u4ef6\u8fdb\u884c\u8ba1\u6570\u65f6\uff0c<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u8ba1\u6570\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u9636\u4e58\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">n = 5<\/p>\n<p>result = 1<\/p>\n<p>while n &gt; 0:<\/p>\n<p>    result *= n<\/p>\n<p>    n -= 1<\/p>\n<p>print(&quot;Factorial is:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001<code>while<\/code>\u5faa\u73af\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9632\u6b62\u65e0\u9650\u5faa\u73af<\/strong>\uff1a\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u5728\u67d0\u4e2a\u65f6\u523b\u4f1a\u53d8\u4e3a\u5047\uff0c\u6216\u8005\u5728\u5faa\u73af\u4f53\u4e2d\u6709\u9002\u5f53\u7684\u9000\u51fa\u673a\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5faa\u73af\u4f53\u4e2d\u907f\u514d\u6267\u884c\u8fc7\u4e8e\u590d\u6742\u6216\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u4ee5\u514d\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u5728\u5faa\u73af\u4f53\u4e2d\u4f7f\u7528\u9002\u5f53\u7684\u53d8\u91cf\u540d\u548c\u6ce8\u91ca\uff0c\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6761\u4ef6\u5224\u65ad<\/strong>\uff1a\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u7684\u5224\u65ad\u903b\u8f91\u6b63\u786e\uff0c\u4ee5\u907f\u514d\u610f\u5916\u7684\u5faa\u73af\u884c\u4e3a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e03\u3001<code>while<\/code>\u5faa\u73af\u4e0e<code>for<\/code>\u5faa\u73af\u7684\u6bd4\u8f83<\/p>\n<\/p>\n<p><p><code>while<\/code>\u5faa\u73af\u4e0e<code>for<\/code>\u5faa\u73af\u662fPython\u4e2d\u4e24\u79cd\u5e38\u7528\u7684\u5faa\u73af\u7ed3\u6784\uff0c\u5404\u6709\u5176\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong><code>while<\/code>\u5faa\u73af<\/strong>\uff1a\u9002\u7528\u4e8e\u5faa\u73af\u6761\u4ef6\u4e0d\u786e\u5b9a\u6216\u9700\u8981\u52a8\u6001\u8c03\u6574\u7684\u60c5\u51b5\u3002\u5176\u5faa\u73af\u6b21\u6570\u4f9d\u8d56\u4e8e\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong><code>for<\/code>\u5faa\u73af<\/strong>\uff1a\u9002\u7528\u4e8e\u5faa\u73af\u6b21\u6570\u5df2\u77e5\u6216\u904d\u5386\u96c6\u5408\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\uff09\u65f6\u3002\u5176\u5faa\u73af\u6b21\u6570\u901a\u5e38\u662f\u56fa\u5b9a\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u867d\u7136<code>while<\/code>\u5faa\u73af\u66f4\u7075\u6d3b\uff0c\u4f46\u5728\u53ef\u9884\u89c1\u7684\u56fa\u5b9a\u5faa\u73af\u60c5\u51b5\u4e0b\uff0c<code>for<\/code>\u5faa\u73af\u901a\u5e38\u66f4\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001<code>while<\/code>\u5faa\u73af\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u9ad8\u7ea7\u7f16\u7a0b\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6Python\u7279\u6027\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5d4c\u5957\u5faa\u73af<\/strong>\uff1a\u5728<code>while<\/code>\u5faa\u73af\u4e2d\u5d4c\u5957\u53e6\u4e00\u4e2a\u5faa\u73af\uff0c\u7528\u4e8e\u5904\u7406\u591a\u5c42\u6b21\u6570\u636e\u7ed3\u6784\u6216\u590d\u6742\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">rows = 3<\/p>\n<p>cols = 3<\/p>\n<p>matrix = [[0]*cols for _ in range(rows)]<\/p>\n<p>i = 0<\/p>\n<p>while i &lt; rows:<\/p>\n<p>    j = 0<\/p>\n<p>    while j &lt; cols:<\/p>\n<p>        matrix[i][j] = i * cols + j<\/p>\n<p>        j += 1<\/p>\n<p>    i += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u751f\u6210\u5668<\/strong>\uff1a\u7ed3\u5408<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u521b\u5efa\u751f\u6210\u5668\uff0c\u5b9e\u73b0\u60f0\u6027\u8ba1\u7b97\u3002\u751f\u6210\u5668\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u8282\u7701\u5185\u5b58\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def infinite_sequence():<\/p>\n<p>    num = 0<\/p>\n<p>    while True:<\/p>\n<p>        yield num<\/p>\n<p>        num += 1<\/p>\n<p>gen = infinite_sequence()<\/p>\n<p>for i in range(10):<\/p>\n<p>    print(next(gen))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u591a\u7ebf\u7a0b<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u5b9e\u73b0\u7ebf\u7a0b\u7684\u6301\u7eed\u8fd0\u884c\uff0c\u76f4\u5230\u67d0\u4e2a\u6761\u4ef6\u89e6\u53d1\u7ebf\u7a0b\u9000\u51fa\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u65f6\u5668\u7ebf\u7a0b\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def timer():<\/p>\n<p>    start_time = time.time()<\/p>\n<p>    while True:<\/p>\n<p>        time.sleep(1)<\/p>\n<p>        print(&quot;Elapsed time:&quot;, int(time.time() - start_time), &quot;seconds&quot;)<\/p>\n<p>timer_thread = threading.Thread(target=timer)<\/p>\n<p>timer_thread.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001<code>while<\/code>\u5faa\u73af\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u9700\u8981\u53ca\u65f6\u89e3\u51b3\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65e0\u9650\u5faa\u73af<\/strong>\uff1a\u5faa\u73af\u6761\u4ef6\u59cb\u7ec8\u4e3a\u771f\uff0c\u5bfc\u81f4\u7a0b\u5e8f\u5361\u6b7b\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u68c0\u67e5\u5faa\u73af\u6761\u4ef6\u548c\u5faa\u73af\u4f53\u5185\u7684\u53d8\u91cf\u53d8\u5316\uff0c\u786e\u4fdd\u6761\u4ef6\u6700\u7ec8\u4f1a\u53d8\u4e3a\u5047\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6761\u4ef6\u5224\u65ad\u9519\u8bef<\/strong>\uff1a\u5faa\u73af\u6761\u4ef6\u5224\u65ad\u4e0d\u5f53\uff0c\u5bfc\u81f4\u5faa\u73af\u672a\u6309\u9884\u671f\u6267\u884c\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4ed4\u7ec6\u68c0\u67e5\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u786e\u4fdd\u903b\u8f91\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d44\u6e90\u5360\u7528<\/strong>\uff1a\u5faa\u73af\u4f53\u5185\u64cd\u4f5c\u8017\u65f6\u6216\u5360\u7528\u5927\u91cf\u8d44\u6e90\uff0c\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4f18\u5316\u5faa\u73af\u4f53\u5185\u7684\u64cd\u4f5c\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u6216\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u4f7f\u7528\u7ebf\u7a0b\u9501\uff08\u5982<code>threading.Lock<\/code>\uff09\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5341\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684<code>while<\/code>\u5faa\u73af\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u9700\u8981\u91cd\u590d\u6267\u884c\u64cd\u4f5c\u7684\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u5faa\u73af\u6761\u4ef6\u3001\u7f16\u5199\u6709\u6548\u7684\u5faa\u73af\u4f53\uff0c\u5e76\u63a7\u5236\u5faa\u73af\u7ed3\u675f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u7a33\u5b9a\u7684\u7a0b\u5e8f\u903b\u8f91\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u5176\u4ed6Python\u7279\u6027\uff0c<code>while<\/code>\u5faa\u73af\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u529f\u80fd\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u907f\u514d\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0cwhile\u5faa\u73af\u7684\u57fa\u672c\u7ed3\u6784\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>while\u5faa\u73af\u662f\u4e00\u79cd\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u7528\u4e8e\u5728\u67d0\u4e2a\u6761\u4ef6\u4e3a\u771f\u65f6\u91cd\u590d\u6267\u884c\u4e00\u6bb5\u4ee3\u7801\u3002\u5176\u57fa\u672c\u7ed3\u6784\u4e3a\uff1a  <\/p>\n<pre><code class=\"language-python\">while condition:\n    # \u6267\u884c\u7684\u4ee3\u7801\n<\/code><\/pre>\n<p>\u5f53\u6761\u4ef6\u4e3a\u771f\u65f6\uff0c\u4ee3\u7801\u5757\u5c06\u6301\u7eed\u6267\u884c\uff0c\u76f4\u5230\u6761\u4ef6\u53d8\u4e3a\u5047\u3002<\/p>\n<p><strong>\u5982\u4f55\u907f\u514dPython\u4e2dwhile\u5faa\u73af\u5bfc\u81f4\u7684\u65e0\u9650\u5faa\u73af\uff1f<\/strong><br \/>\u4e3a\u4e86\u907f\u514d\u65e0\u9650\u5faa\u73af\uff0c\u786e\u4fdd\u5728\u5faa\u73af\u4f53\u5185\u6709\u903b\u8f91\u6765\u6539\u53d8\u5faa\u73af\u6761\u4ef6\u7684\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u5728\u5faa\u73af\u4e2d\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u6216\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\u3002\u826f\u597d\u7684\u5b9e\u8df5\u662f\u4f7f\u7528\u8ba1\u6570\u5668\u6216\u72b6\u6001\u53d8\u91cf\u8fdb\u884c\u63a7\u5236\uff0c\u786e\u4fdd\u5faa\u73af\u5728\u9884\u671f\u65f6\u95f4\u5185\u7ed3\u675f\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528while\u5faa\u73af\u65f6\uff0c\u5982\u4f55\u5b9e\u73b0\u8ba1\u6570\u529f\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u8ba1\u6570\u5668\u53d8\u91cf\u5e76\u5728\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u4e2d\u5bf9\u5176\u8fdb\u884c\u66f4\u65b0\u6765\u5b9e\u73b0\u8ba1\u6570\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">count = 0\nwhile count &lt; 5:\n    print(&quot;\u5f53\u524d\u8ba1\u6570:&quot;, count)\n    count += 1\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\u4ece0\u52304\u7684\u8ba1\u6570\uff0c\u5e76\u5728\u8ba1\u6570\u8fbe\u52305\u65f6\u505c\u6b62\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728while\u5faa\u73af\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\uff1f<\/strong><br \/>\u5728while\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u63a5\u6536\u7528\u6237\u8f93\u5165\uff0c\u5e76\u6839\u636e\u8f93\u5165\u7684\u5185\u5bb9\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">user_input = &quot;&quot;\nwhile user_input.lower() != &quot;exit&quot;:\n    user_input = input(&quot;\u8bf7\u8f93\u5165\u547d\u4ee4\uff08\u8f93\u5165&#39;exit&#39;\u9000\u51fa\uff09\uff1a&quot;)\n    print(&quot;\u4f60\u8f93\u5165\u7684\u547d\u4ee4\u662f:&quot;, user_input)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u7528\u6237\u5728\u5faa\u73af\u4e2d\u8f93\u5165\u591a\u6761\u547d\u4ee4\uff0c\u76f4\u81f3\u8f93\u5165\u7279\u5b9a\u7684\u9000\u51fa\u6307\u4ee4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528while\u5faa\u73af\u7684\u65b9\u6cd5\u662f\uff1a\u5b9a\u4e49\u5faa\u73af\u6761\u4ef6\u3001\u4f7f\u7528while\u8bed\u53e5\u3001\u7f16\u5199\u5faa\u73af\u4f53\u3001\u63a7\u5236\u5faa\u73af\u7ed3\u675f\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":974363,"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\/974358"}],"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=974358"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974358\/revisions"}],"predecessor-version":[{"id":974367,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974358\/revisions\/974367"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974363"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}