{"id":179171,"date":"2024-05-08T20:07:22","date_gmt":"2024-05-08T12:07:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179171.html"},"modified":"2024-05-08T20:07:28","modified_gmt":"2024-05-08T12:07:28","slug":"python%e4%b8%adwhile%e5%90%8e%e7%9a%84%e6%9d%a1%e4%bb%b6%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e7%b1%bb%e5%9e%8b%e5%8f%af%e4%bb%a5%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179171.html","title":{"rendered":"Python\u4e2dwhile\u540e\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063837\/cb7dbde0-5592-4b82-a1c5-c819afab3bd2.webp\" alt=\"Python\u4e2dwhile\u540e\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>while<\/code>\u5faa\u73af\u540e\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f<strong>\u4efb\u4f55\u5e03\u5c14\u8868\u8fbe\u5f0f<\/strong>\u3001<strong>\u5177\u6709\u5e03\u5c14\u503c\u7684\u53d8\u91cf<\/strong>\u3001<strong>\u4efb\u4f55\u6570\u503c\u7c7b\u578b<\/strong>\u3001<strong>\u5bb9\u5668\u7c7b\u578b<\/strong>\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178\uff09\u4ee5\u53ca<strong>\u4efb\u4f55\u53ef\u4ee5\u8f6c\u6362\u6210\u5e03\u5c14\u503c\u7684\u5bf9\u8c61<\/strong>\u3002<strong>Python\u4f1a\u5c06\u8fd9\u4e9b\u8868\u8fbe\u5f0f\u89e3\u91ca\u4e3aTrue\u6216False<\/strong>\u6765\u51b3\u5b9a\u662f\u5426\u7ee7\u7eed\u6267\u884c\u5faa\u73af\u3002\u5176\u4e2d\uff0c\u6570\u503c\u4e2d\u7684<code>0<\/code>\u3001\u7a7a\u5bb9\u5668\u548c<code>None<\/code>\u7b49\u7279\u6b8a\u503c\u4f1a\u88ab\u89e3\u91ca\u4e3a<code>False<\/code>\uff0c\u800c\u5176\u4ed6\u503c\u5219\u88ab\u8ba4\u4e3a\u662f<code>True<\/code>\u3002\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u975e\u5e03\u5c14\u7c7b\u578b\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u5176\u5e03\u5c14\u503c\u89e3\u91ca\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u903b\u8f91\u9519\u8bef\u6216\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528\u53ca\u5176\u8f6c\u6362\u4e3a\u5e03\u5c14\u503c\u7684\u89c4\u5219\u548c\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5e03\u5c14\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p><strong>\u5e03\u5c14\u8868\u8fbe\u5f0f<\/strong>\u662f\u6700\u5e38\u7528\u7684<code>while<\/code>\u5faa\u73af\u6761\u4ef6\u3002Python\u4e2d\u7684\u5e03\u5c14\u8868\u8fbe\u5f0f\u53ef\u4ee5\u76f4\u63a5\u662f<code>True<\/code>\u6216<code>False<\/code>\uff0c\u4e5f\u53ef\u4ee5\u662f\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3001\u903b\u8f91\u8fd0\u7b97\u7b26\u548c\u8eab\u4efd\u8fd0\u7b97\u7b26\u7b49\u7684\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><h4>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/h4>\n<\/p>\n<p><p>\u6bd4\u8f83\u8fd0\u7b97\u7b26\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5982<code>==<\/code>\u3001<code>!=<\/code>\u3001<code>&gt;<\/code>\u3001<code>&lt;<\/code>\u3001<code>&gt;=<\/code>\u3001<code>&lt;=<\/code>\u3002\u5f53\u6bd4\u8f83\u6210\u7acb\u65f6\uff0c\u6bd4\u8f83\u8fd0\u7b97\u7b26\u7684\u7ed3\u679c\u4e3a<code>True<\/code>\uff0c\u5426\u5219\u4e3a<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h4>\u903b\u8f91\u8fd0\u7b97\u7b26<\/h4>\n<\/p>\n<p><p>\u903b\u8f91\u8fd0\u7b97\u7b26\u7528\u4e8e\u5c06\u591a\u4e2a\u6bd4\u8f83\u8fd0\u7b97\u7ec4\u5408\u8d77\u6765\u505a\u51fa\u590d\u6742\u7684\u903b\u8f91\u5224\u65ad\uff0c\u5305\u62ec<code>and<\/code>\u3001<code>or<\/code>\u3001<code>not<\/code>\u3002\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u7528\u4e8e\u63a7\u5236\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u6574\u4f53\u5e03\u5c14\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5177\u6709\u5e03\u5c14\u503c\u7684\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u5faa\u73af\u6761\u4ef6\u4e2d\u76f4\u63a5\u4f7f\u7528\u53d8\u91cf\u4e5f\u662f\u975e\u5e38\u5e38\u89c1\u7684\uff0c<strong>\u8fd9\u91cc\u7684\u53d8\u91cf\u5e94\u8be5\u662f\u4e00\u4e2a\u8d4b\u4e88\u4e86\u5e03\u5c14\u503c\u7684\u8868\u8fbe\u5f0f<\/strong>\u6216\u5176\u4ed6\u5177\u6709\u660e\u786e\u5e03\u5c14\u542b\u4e49\u7684\u503c\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u7528\u4e8e\u63a7\u5236\u5faa\u73af\u7684\u6807\u5fd7\u4f4d\uff08flag\uff09\u901a\u5e38\u662f\u4e00\u4e2a\u5177\u6709\u5e03\u5c14\u503c\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6570\u503c\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u4efb\u4f55<strong>\u6570\u503c\u7c7b\u578b<\/strong>\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u548c\u590d\u6570\uff0c\u5728Python\u4e2d\u90fd\u53ef\u4ee5\u4f5c\u4e3a<code>while<\/code>\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002\u8fd9\u662f\u56e0\u4e3aPython\u53ef\u4ee5\u5c06\u6570\u503c\u7c7b\u578b\u81ea\u52a8\u8f6c\u6362\u6210\u5e03\u5c14\u503c\uff1a\u4efb\u4f55\u975e\u96f6\u6570\u503c\u90fd\u88ab\u89e3\u91ca\u4e3a<code>True<\/code>\uff0c\u800c\u96f6\u503c\uff08<code>0<\/code>\u3001<code>0.0<\/code>\u3001<code>0j<\/code>\uff09\u88ab\u89e3\u91ca\u4e3a<code>False<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5bb9\u5668\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5bb9\u5668\u7c7b\u578b\uff0c\u5982<strong>\u5217\u8868\u3001\u5143\u7ec4\u3001\u96c6\u5408\u3001\u5b57\u5178<\/strong>\u7b49\uff0c\u4e5f\u53ef\u4ee5\u7528\u4f5c<code>while<\/code>\u5faa\u73af\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u3002\u5f53\u5bb9\u5668\u662f\u7a7a\u7684\u65f6\u5019\uff08\u5373\u6ca1\u6709\u5143\u7d20\uff09\uff0c\u5b83\u4f1a\u88ab\u89e3\u91ca\u4e3a<code>False<\/code>\u3002\u5982\u679c\u5bb9\u5668\u81f3\u5c11\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff0c\u65e0\u8bba\u5143\u7d20\u662f\u4ec0\u4e48\uff0c\u5bb9\u5668\u90fd\u88ab\u89e3\u91ca\u4e3a<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u53ef\u8f6c\u6362\u6210\u5e03\u5c14\u503c\u7684\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u51e0\u4e4e\u6240\u6709Python\u5bf9\u8c61\u90fd\u53ef\u4ee5\u8f6c\u6362\u6210\u5e03\u5c14\u503c\uff0c\u7279\u522b\u662f\u6240\u6709\u5bf9\u8c61\u9ed8\u8ba4\u90fd\u63d0\u4f9b\u4e86<code>.__bool__()<\/code>\u6216<code>.__len__()<\/code>\u65b9\u6cd5\u6765\u8ba9Python\u5224\u5b9a\u5176\u5e03\u5c14\u503c\u3002\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u7684<code>__bool__<\/code>\u65b9\u6cd5\u8fd4\u56de<code>True<\/code>\u6216\u8005<code>__len__<\/code>\u65b9\u6cd5\u8fd4\u56de\u975e\u96f6\u503c\uff0c\u5219\u8be5\u5bf9\u8c61\u5728\u5e03\u5c14\u4e0a\u4e0b\u6587\u4e2d\u88ab\u8ba4\u4e3a\u662f<code>True<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u90e8\u5206\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5728<code>while<\/code>\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u4e0a\u8ff0\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u5e03\u5c14\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">condition = True<\/p>\n<p>while condition:<\/p>\n<p>    # \u6b64\u5904\u662f\u5faa\u73af\u4f53<\/p>\n<p>    # ... \u8fd9\u91cc\u53ef\u4ee5\u6709\u4ee3\u7801\u6765\u6539\u53d8condition\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u6570\u503c\u7c7b\u578b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">counter = 5<\/p>\n<p>while counter:<\/p>\n<p>    # \u6bcf\u6b21\u5faa\u73af\uff0c\u8ba1\u6570\u51cf1<\/p>\n<p>    counter -= 1<\/p>\n<p>    # ... \u5176\u4ed6\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5bb9\u5668\u7c7b\u578b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4]<\/p>\n<p>while items:<\/p>\n<p>    # \u6bcf\u6b21\u5faa\u73af\u4ece\u5217\u8868\u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>    items.pop()<\/p>\n<p>    # ... \u5176\u4ed6\u4ee3\u7801<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u5e26\u6709\u5e03\u5c14\u503c\u5224\u5b9a\u7684\u5bf9\u8c61<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyCont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ner:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def __bool__(self):<\/p>\n<p>        return bool(self.data)<\/p>\n<p>my_container = MyContainer([1, 2, 3])<\/p>\n<p>while my_container:<\/p>\n<p>    # \u4f1a\u8c03\u7528my_container.__bool__()\u6765\u5224\u65ad\u5faa\u73af\u662f\u5426\u7ee7\u7eed<\/p>\n<p>    # ... \u5176\u4ed6\u4ee3\u7801<\/p>\n<p>    # \u5047\u8bbe\u8fd9\u91cc\u6709\u903b\u8f91\u4fee\u6539\u4e86my_container\u7684data\u5c5e\u6027<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u5c31\u662f\u5728Python\u4e2d<code>while<\/code>\u5faa\u73af\u540e\u53ef\u4f7f\u7528\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\uff0c\u4e86\u89e3\u8fd9\u4e9b\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u6709\u6548\u5730\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u63a7\u5236\u7a0b\u5e8f\u903b\u8f91\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: \u5728Python\u4e2d\uff0cwhile\u5faa\u73af\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4ec0\u4e48\u7c7b\u578b\uff1f<\/strong><br \/>\nA1: Python\u4e2d\u7684while\u5faa\u73af\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5373True\u6216False\u3002\u4f46\u5b83\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u4e32\u6216\u5217\u8868\u3002\u5f53\u6761\u4ef6\u8868\u8fbe\u5f0f\u7684\u503c\u4e3a\u975e\u96f6\u6570\u5b57\u3001\u975e\u7a7a\u5b57\u7b26\u4e32\u6216\u975e\u7a7a\u5217\u8868\u65f6\uff0c\u5faa\u73af\u5c06\u4f1a\u4e00\u76f4\u6267\u884c\u4e0b\u53bb\u3002<\/p>\n<p><strong>Q2: Python\u4e2dwhile\u5faa\u73af\u7684\u6761\u4ef6\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u5417\uff1f<\/strong><br \/>\nA2: \u662f\u7684\uff0cPython\u4e2d\u7684while\u5faa\u73af\u7684\u6761\u4ef6\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u8fed\u4ee3\u65f6\uff0c\u7a0b\u5e8f\u5c06\u4f1a\u8c03\u7528\u8be5\u51fd\u6570\u6765\u8bc4\u4f30\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3aTrue\uff0c\u5219\u5faa\u73af\u7ee7\u7eed\u6267\u884c\uff1b\u5982\u679c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3aFalse\uff0c\u5219\u5faa\u73af\u505c\u6b62\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u5728\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u81ea\u5b9a\u4e49\u7684\u903b\u8f91\u548c\u5224\u65ad\u3002<\/p>\n<p><strong>Q3: \u5728Python\u4e2d\uff0c\u4f7f\u7528while\u5faa\u73af\u65f6\u5e94\u8be5\u907f\u514d\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\nA3: \u5728\u4f7f\u7528while\u5faa\u73af\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5ffd\u7565\u66f4\u65b0\u5faa\u73af\u6761\u4ef6\u3001\u6ca1\u6709\u8bbe\u7f6e\u5faa\u73af\u7ec8\u6b62\u6761\u4ef6\u3001\u5faa\u73af\u7684\u9000\u51fa\u6761\u4ef6\u4e0e\u5b9e\u9645\u9700\u6c42\u4e0d\u7b26\u7b49\u3002\u4e3a\u907f\u514d\u8fd9\u4e9b\u9519\u8bef\uff0c\u53ef\u4ee5\u5728\u8fdb\u5165\u5faa\u73af\u4f53\u4e4b\u524d\u786e\u4fdd\u5faa\u73af\u6761\u4ef6\u7684\u6b63\u786e\u6027\uff0c\u5e76\u5728\u5faa\u73af\u4f53\u5185\u66f4\u65b0\u5faa\u73af\u6761\u4ef6\u7684\u503c\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u907f\u514d\u51fa\u73b0\u65e0\u9650\u5faa\u73af\uff0c\u5e94\u8be5\u786e\u4fdd\u5faa\u73af\u7ec8\u6b62\u6761\u4ef6\u6700\u7ec8\u4f1a\u5f97\u5230\u6ee1\u8db3\u3002\u53ef\u4ee5\u4f7f\u7528\u6807\u5fd7\u53d8\u91cf\u3001break\u8bed\u53e5\u6216\u5176\u4ed6\u9002\u5f53\u7684\u6280\u5de7\u6765\u5b9e\u73b0\u5faa\u73af\u7684\u6b63\u786e\u9000\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0cwhile\u5faa\u73af\u540e\u7684\u6761\u4ef6\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u4f55\u5e03\u5c14\u8868\u8fbe\u5f0f\u3001\u5177\u6709\u5e03\u5c14\u503c\u7684\u53d8\u91cf\u3001\u4efb\u4f55\u6570\u503c\u7c7b\u578b\u3001\u5bb9\u5668\u7c7b\u578b\uff08 [&hellip;]","protected":false},"author":3,"featured_media":179177,"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\/179171"}],"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=179171"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179171\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179177"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}