{"id":70,"date":"2026-08-05T23:50:43","date_gmt":"2026-08-05T15:50:43","guid":{"rendered":"https:\/\/ltrainyard.icu\/?p=70"},"modified":"2026-08-06T00:42:24","modified_gmt":"2026-08-05T16:42:24","slug":"day-5-%e5%be%aa%e7%8e%af%ef%bc%9afor-while-range-break-continue-%e4%b8%8e%e5%b5%8c%e5%a5%97%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/ltrainyard.icu\/index.php\/2026\/08\/05\/day-5-%e5%be%aa%e7%8e%af%ef%bc%9afor-while-range-break-continue-%e4%b8%8e%e5%b5%8c%e5%a5%97%e5%be%aa%e7%8e%af\/","title":{"rendered":"| Day 5 | \u5faa\u73af\uff1afor \/ while \/ range \/ break \/ continue \u4e0e\u5d4c\u5957\u5faa\u73af |"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1 \u5faa\u73af\u7684\u672c\u8d28 \u2014\u2014 \u628a\u91cd\u590d\u52b3\u52a8\u4ea4\u7ed9\u7a0b\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5faa\u73af<\/strong>\uff08loop\uff09\u662f\u8ba9\u7a0b\u5e8f<strong>\u53cd\u590d\u6267\u884c\u540c\u4e00\u6bb5\u4ee3\u7801<\/strong>\u7684\u8bed\u6cd5\u7ed3\u6784\u3002\u5199\u4ee3\u7801\u65f6\uff0c80% \u7684\u300c\u91cd\u590d\u300d\u5de5\u4f5c\u90fd\u80fd\u7528\u5faa\u73af\u89e3\u51b3\uff1a\u6253\u5370 100 \u6b21\u3001\u7d2f\u52a0 100 \u4e2a\u6570\u3001\u5904\u7406 100 \u4e2a\u6587\u4ef6\u2026\u2026\u4e0d\u4f1a\u5faa\u73af\uff0c\u4ee3\u7801\u53c8\u81ed\u53c8\u957f\uff1b\u5b66\u4f1a\u5faa\u73af\uff0c\u4ee3\u7801\u91cf\u76f4\u63a5\u51cf\u5c11\u4e00\u4e2a\u6570\u91cf\u7ea7\u3002\u5982\u679c\u8ba9\u4f60\u6253\u5370 100 \u904d\u300c\u52a0\u6cb9\u300d\uff0c\u4f60\u4e0d\u4f1a\u771f\u7684\u5199 100 \u884c <code>print<\/code>\u2014\u2014\u5faa\u73af\u5c31\u662f\u8ba9\u7a0b\u5e8f\u300c\u81ea\u52a8\u91cd\u590d\u300d\u7684\u8bed\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba1\u7b97\u673a\u7684 CPU \u672c\u8d28\u4e0a\u662f\u300c\u4e00\u6761\u4e00\u6761\u6267\u884c\u6307\u4ee4\u300d\u7684\u673a\u5668\uff0c\u5b83\u5185\u90e8\u6709\u4e00\u4e2a\u300c\u6307\u4ee4\u6307\u9488\u300d\uff0c\u8bb0\u5f55\u4e0b\u4e00\u6761\u8be5\u6267\u884c\u54ea\u6761\u6307\u4ee4\u3002\u5faa\u73af\u7684\u539f\u7406\u5c31\u85cf\u5728\u8fd9\u91cc\uff1a\u5faa\u73af\u4f53\u6267\u884c\u5b8c\u540e\uff0c\u6307\u4ee4\u6307\u9488\u4e0d\u662f\u7ee7\u7eed\u5f80\u4e0b\u8d70\uff0c\u800c\u662f<strong>\u8df3\u56de<\/strong>\u5faa\u73af\u5f00\u5934\uff0c\u518d\u6b21\u5224\u65ad\u6761\u4ef6 \/ \u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\uff1b\u76f4\u5230\u6761\u4ef6\u4e0d\u6210\u7acb\u6216\u5143\u7d20\u53d6\u5b8c\uff0c\u624d\u8df3\u51fa\u5faa\u73af\u7ee7\u7eed\u5f80\u540e\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u50cf\u4e00\u53f0<strong>\u590d\u8bfb\u673a<\/strong>\uff1a\u6309\u4e00\u4e0b\u300c\u64ad\u653e\u300d\uff0c\u5b83\u5c31\u628a\u540c\u4e00\u53e5\u8bdd\u7ffb\u6765\u8986\u53bb\u5730\u5ff5\u3002\u590d\u8bfb\u673a\u7684\u300c\u6b21\u6570\u65cb\u94ae\u300d\u5b9a\u597d\u5ff5\u51e0\u904d\uff0c\u5faa\u73af\u91cc\u7684 <code>range()<\/code> \u4e5f\u5b9a\u597d\u6267\u884c\u51e0\u6b21\u2014\u2014\u628a\u300c\u91cd\u590d\u52b3\u52a8\u300d\u4ea4\u7ed9\u673a\u5668\u81ea\u52a8\u5b8c\u6210\uff0c\u8fd9\u6b63\u662f\u5faa\u73af\u5b58\u5728\u7684\u610f\u4e49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5faa\u73af\u7248 vs \u624b\u5199\u7248<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u624b\u5199\u7248\uff1a\u6253\u5370 3 \u904d\u8981\u5199 3 \u884c\nprint(\"\u52a0\u6cb9\")\nprint(\"\u52a0\u6cb9\")\nprint(\"\u52a0\u6cb9\")\n\n# \u5faa\u73af\u7248\uff1a\u60f3\u6253\u5370 100 \u904d\u4e5f\u53ea\u662f\u6539\u4e00\u4e2a\u6570\u5b57\nfor i in range(3):\n    print(\"\u52a0\u6cb9\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u4e24\u8005\u4e00\u81f4\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u52a0\u6cb9\n\u52a0\u6cb9\n\u52a0\u6cb9\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>for i in range(3):<\/code> \u505a\u4e86\u4e24\u4ef6\u4e8b\u2014\u2014<code>range(3)<\/code> \u751f\u6210 0\u30011\u30012 \u4e09\u4e2a\u6570\u5b57\uff1b<code>for i in<\/code> \u8ba9\u53d8\u91cf i \u4f9d\u6b21\u53d6\u8fd9\u4e09\u4e2a\u503c\uff0c\u6bcf\u53d6\u4e00\u4e2a\u503c\u5c31\u6267\u884c\u4e00\u6b21\u7f29\u8fdb\u7684\u4ee3\u7801\uff0c\u53d6\u5b8c 3 \u4e2a\u503c\u5faa\u73af\u81ea\u52a8\u7ed3\u675f\u3002\u884c\u9996\u7684\u7f29\u8fdb\uff084 \u4e2a\u7a7a\u683c\uff09\u544a\u8bc9 Python\u300c\u54ea\u4e9b\u4ee3\u7801\u5c5e\u4e8e\u5faa\u73af\u4f53\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u5faa\u73af\u53d8\u91cf i \u7684\u503c\u662f\u600e\u4e48\u53d8\u5316\u7684<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(4):\n    print(\"\u7b2c\", i, \"\u6b21\u5faa\u73af\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c 0 \u6b21\u5faa\u73af\n\u7b2c 1 \u6b21\u5faa\u73af\n\u7b2c 2 \u6b21\u5faa\u73af\n\u7b2c 3 \u6b21\u5faa\u73af\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\u4ece 0 \u5f00\u59cb\uff01\u6bcf\u8f6e\u5faa\u73af\u5f00\u59cb\u524d\uff0cPython \u81ea\u52a8\u628a\u4e0b\u4e00\u4e2a\u503c\u653e\u8fdb\u53d8\u91cf i\uff0c<strong>\u4e0d\u9700\u8981\u6211\u4eec\u624b\u52a8\u53bb\u6539 i<\/strong>\uff0c\u8fd9\u662f for \u5faa\u73af\u548c while \u5faa\u73af\u6700\u4e0d\u4e00\u6837\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u5faa\u73af + \u53d8\u91cf = \u6279\u91cf\u8ba1\u7b97<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 6):\n    print(i, \"\u7684\u5e73\u65b9\u662f\", i * i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 \u7684\u5e73\u65b9\u662f 1\n2 \u7684\u5e73\u65b9\u662f 4\n3 \u7684\u5e73\u65b9\u662f 9\n4 \u7684\u5e73\u65b9\u662f 16\n5 \u7684\u5e73\u65b9\u662f 25\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u4f53\u91cc\u4e0d\u4ec5\u80fd\u6253\u5370\uff0c\u8fd8\u80fd\u505a\u4efb\u4f55\u8ba1\u7b97\u2014\u2014\u8fd9\u5c31\u662f\u5faa\u73af\u300c\u6279\u91cf\u5904\u7406\u300d\u7684\u5a01\u529b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>for i in range(3): print(i)<\/code> \u6324\u5728\u4e00\u884c \u2192 \u95ee\u9898\uff1a\u867d\u7136\u80fd\u8fd0\u884c\uff0c\u4f46\u5faa\u73af\u4f53\u4e00\u591a\u884c\u5c31\u5bb9\u6613\u4e71\uff0c\u517b\u6210\u574f\u4e60\u60ef \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5faa\u73af\u4f53\u7edf\u4e00\u6362\u884c + \u7f29\u8fdb 4 \u4e2a\u7a7a\u683c\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5faa\u73af\u4f53\u4e0d\u7f29\u8fdb\uff0c\u4e0b\u4e00\u884c\u76f4\u63a5\u9876\u683c\u5199 \u2192 \u95ee\u9898\uff1a\u62a5 <code>IndentationError<\/code>\uff0cPython \u5206\u4e0d\u6e05\u54ea\u4e9b\u4ee3\u7801\u5c5e\u4e8e\u5faa\u73af \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5faa\u73af\u4f53\u5185\u6bcf\u4e00\u884c\u90fd\u6bd4 for \u884c\u591a\u7f29\u8fdb 4 \u4e2a\u7a7a\u683c\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u4ee5\u4e3a <code>range(3)<\/code> \u662f 1\u30012\u30013 \u2192 \u95ee\u9898\uff1a\u5b9e\u9645\u662f 0\u30011\u30012\uff0c\u5faa\u73af\u4f53\u4f1a\u6267\u884c 3 \u6b21 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6570\u300c\u5faa\u73af\u51e0\u6b21\u300d\u53ea\u770b range \u91cc\u7684\u6570\u5b57\uff1a<code>range(n)<\/code> = n \u6b21\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u7f29\u8fdb\u4f53\u5185\u53cd\u590d\u5ff5\uff1brange \u5b9a\u6b21\u6570\uff0c\u53d8\u91cf\u6bcf\u6b21\u6362\u65b0\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2 range() \u7684\u79d8\u5bc6 \u2014\u2014 \u542b\u5934\u4e0d\u542b\u5c3e\u4e0e\u6b65\u957f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range()<\/code> \u662f Python \u5185\u7f6e\u7684\u300c\u6570\u5b57\u5e8f\u5217\u751f\u6210\u5668\u300d\uff0c\u7528\u6765\u544a\u8bc9 for \u5faa\u73af\u300c\u5faa\u73af\u51e0\u6b21\u3001\u4ece\u51e0\u5f00\u59cb\u3001\u9694\u51e0\u4e2a\u53d6\u4e00\u4e2a\u300d\u3002\u5b83\u662f\u5faa\u73af\u6b21\u6570\u63a7\u5236\u7684\u6838\u5fc3\u5de5\u5177\uff0c\u51e0\u4e4e\u6bcf\u4e2a\u5faa\u73af\u90fd\u4f1a\u7528\u5230\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range()<\/code> \u4e0d\u4f1a\u628a\u4e00\u4e32\u6570\u5b57\u5168\u90e8\u751f\u6210\u51fa\u6765\u5b58\u653e\u5728\u5185\u5b58\u91cc\uff08\u5faa\u73af 1 \u4ebf\u6b21\u4f1a\u6491\u7206\u5185\u5b58\uff09\uff0c\u800c\u662f\u300c\u60f0\u6027\u751f\u6210\u300d\uff1a\u6bcf\u6b21\u9700\u8981\u4e0b\u4e00\u4e2a\u6570\u65f6\uff0c\u6309\u516c\u5f0f\uff08\u8d77\u59cb\u503c + \u5f53\u524d\u6b21\u6570 \u00d7 \u6b65\u957f\uff09\u73b0\u573a\u7b97\u51fa\u6765\uff0c\u6240\u4ee5\u5faa\u73af\u591a\u5c11\u6b21\u90fd\u53ea\u5360\u56fa\u5b9a\u5185\u5b58\u3002\u300c\u542b\u5934\u4e0d\u542b\u5c3e\u300d\u662f Python \u5728 range\u3001\u5207\u7247\u3001\u533a\u95f4\u91cc\u7edf\u4e00\u91c7\u7528\u7684\u7ea6\u5b9a\uff0c\u597d\u5904\u662f\uff1a<code>range(n)<\/code> \u6070\u597d\u751f\u6210 n \u4e2a\u6570\uff0c\u4e0e\u300c\u4e0b\u6807\u4ece 0 \u5f00\u59cb\u300d\u5b8c\u7f8e\u914d\u5408\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range<\/code> \u50cf<strong>\u7535\u5f71\u9662\u91cc\u7684\u5ea7\u4f4d\u53f7<\/strong>\uff1a\u4ece\u7b2c\u51e0\u6392\u5f00\u59cb\u3001\u5230\u7b2c\u51e0\u6392\u7ed3\u675f\u3001\u9694\u51e0\u4e2a\u5ea7\u4f4d\u5750\u4e00\u4eba\uff0c\u90fd\u5199\u5f97\u5f88\u6e05\u695a\u3002\u5ea7\u4f4d\u53f7\u6c38\u8fdc\u4ece 0 \u6392\u5f00\u59cb\u6570\uff08\u542b\u5934\uff09\uff0c\u5230\u7ed3\u675f\u53f7\u90a3\u6392<strong>\u4e0d\u5750\u4eba<\/strong>\uff08\u4e0d\u542b\u5c3e\uff09\u2014\u2014\u8bb0\u4f4f\u8fd9\u4e2a\u7ea6\u5b9a\uff0crange \u5c31\u4e0d\u4f1a\u6570\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>range \u7684\u4e09\u79cd\u5f62\u6001<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(list(range(5)))          # 0 \u5230 4\uff1a\u4e00\u4e2a\u53c2\u6570 = \u751f\u6210\u51e0\u4e2a\u6570\nprint(list(range(2, 6)))       # 2 \u5230 5\uff1a\u4e24\u4e2a\u53c2\u6570 = \u4ece\u54ea\u5f00\u59cb\uff0c\u5230\u54ea\u7ed3\u675f\uff08\u4e0d\u542b\u5c3e\uff09\nprint(list(range(1, 10, 3)))   # 1\u30014\u30017\uff1a\u4e09\u4e2a\u53c2\u6570 = \u518d\u52a0\u6b65\u957f\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;0, 1, 2, 3, 4]\n&#91;2, 3, 4, 5]\n&#91;1, 4, 7]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>list(range(...))<\/code> \u662f\u628a range \u751f\u6210\u7684\u5e8f\u5217\u300c\u5c55\u5f00\u300d\u6210\u5217\u8868\u663e\u793a\u51fa\u6765\uff08\u76f4\u63a5 <code>print(range(5))<\/code> \u53ea\u4f1a\u663e\u793a <code>range(0, 5)<\/code> \u8fd9\u4e2a\u5bf9\u8c61\u672c\u8eab\uff09\u3002<code>range(1, 10, 3)<\/code> \u4ece 1 \u5f00\u59cb\u6bcf\u6b21\u52a0 3\uff1a1\u30014\u30017\u300110\u2014\u2014\u4f46 10 \u4e0d\u542b\uff08\u542b\u5934\u4e0d\u542b\u5c3e\uff09\uff0c\u6240\u4ee5\u7ed3\u679c\u662f 1\u30014\u30017\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u6b65\u957f\u4e3a\u8d1f = \u5012\u7740\u6570<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5, 0, -1):\n    print(\"\u5012\u8ba1\u65f6\uff1a\", i)\nprint(\"\u53d1\u5c04\uff01\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5012\u8ba1\u65f6\uff1a 5\n\u5012\u8ba1\u65f6\uff1a 4\n\u5012\u8ba1\u65f6\uff1a 3\n\u5012\u8ba1\u65f6\uff1a 2\n\u5012\u8ba1\u65f6\uff1a 1\n\u53d1\u5c04\uff01\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range(5, 0, -1)<\/code> \u4ece 5 \u5f00\u59cb\uff0c\u6bcf\u6b21\u51cf 1\uff0c\u76f4\u5230 0 \u4e4b\u524d\uff08\u4e0d\u542b 0\uff09\u4e3a\u6b62\u3002<strong>\u6b65\u957f\u662f\u8d1f\u6570\u65f6\uff0c\u8d77\u59cb\u503c\u5fc5\u987b\u5927\u4e8e\u7ed3\u675f\u503c<\/strong>\uff0c\u5426\u5219\u751f\u6210\u7684\u662f\u7a7a\u5e8f\u5217\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>range \u4e0e\u5b57\u7b26\u4e32\u4e0b\u6807\u914d\u5408<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>word = \"Python\"\nfor i in range(len(word)):\n    print(\"\u7b2c\", i, \"\u4e2a\u5b57\u7b26\u662f\uff1a\", word&#91;i])\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c 0 \u4e2a\u5b57\u7b26\u662f\uff1a P\n\u7b2c 1 \u4e2a\u5b57\u7b26\u662f\uff1a y\n\u7b2c 2 \u4e2a\u5b57\u7b26\u662f\uff1a t\n\u7b2c 3 \u4e2a\u5b57\u7b26\u662f\uff1a h\n\u7b2c 4 \u4e2a\u5b57\u7b26\u662f\uff1a o\n\u7b2c 5 \u4e2a\u5b57\u7b26\u662f\uff1a n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>len(word)<\/code> \u5f97\u5230\u5b57\u7b26\u4e32\u957f\u5ea6 6\uff1b<code>range(6)<\/code> \u751f\u6210 0~5\uff1b<code>word[i]<\/code> \u7528\u4e0b\u6807\u53d6\u51fa\u7b2c i \u4e2a\u5b57\u7b26\u3002\u8fd9\u79cd\u300crange + \u4e0b\u6807\u300d\u7684\u7ec4\u5408\uff0c\u5728\u9700\u8981\u300c\u77e5\u9053\u5f53\u524d\u662f\u7b2c\u51e0\u4e2a\u300d\u65f6\u975e\u5e38\u6709\u7528\uff08Day 6 \u904d\u5386\u5217\u8868\u65f6\u8fd8\u4f1a\u89c1\u5230\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>range(5)<\/code> \u4ee5\u4e3a\u751f\u6210 1<del>5 \u2192 \u95ee\u9898\uff1a\u5b9e\u9645\u751f\u6210 0<\/del>4\uff0c\u4e0d\u542b 5 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u60f3\u8981 1~5 \u5199 <code>range(1, 6)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>range(1, 5)<\/code> \u4ee5\u4e3a\u6709 5 \u4e2a\u6570 \u2192 \u95ee\u9898\uff1a\u53ea\u6709 4 \u4e2a\u6570\uff081\u30012\u30013\u30014\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u4e2a\u6570 = \u7ed3\u675f\u503c &#8211; \u5f00\u59cb\u503c\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>range(10, 0, 2)<\/code> \u60f3\u4ece 10 \u6570\u5230 0 \u2192 \u95ee\u9898\uff1a\u751f\u6210\u7684\u662f<strong>\u7a7a\u5e8f\u5217<\/strong>\uff01\u6b65\u957f\u4e3a\u6b63\u8981\u6c42\u5f00\u59cb &lt; \u7ed3\u675f \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5012\u7740\u6570\u8981\u5199 <code>range(10, 0, -2)<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>for i in range(\"abc\")<\/code> \u2192 \u95ee\u9898\uff1a\u62a5 <code>TypeError<\/code>\uff0crange \u53ea\u63a5\u53d7\u6574\u6570 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u6570\u5b57\u7528 range\uff1b\u5b57\u7b26\u4e32\u76f4\u63a5 <code>for ch in \"abc\"<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u542b\u5934\u4e0d\u542b\u5c3e\uff0c\u8d77\u6b65\u770b\u5f00\u59cb\uff1b\u6b65\u957f\u5b9a\u589e\u51cf\uff0c\u5012\u5e8f\u5199\u8d1f\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 while \u5faa\u73af \u2014\u2014 \u6761\u4ef6\u9a71\u52a8\u4e0e\u6b7b\u5faa\u73af\u9632\u8303<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>while<\/code> \u5faa\u73af\u5728\u300c\u6761\u4ef6\u4e3a\u771f\u300d\u671f\u95f4\u4e00\u76f4\u6267\u884c\uff0c\u9002\u5408<strong>\u4e0d\u77e5\u9053\u8981\u5faa\u73af\u591a\u5c11\u6b21<\/strong>\u7684\u573a\u666f\uff1a\u6bd4\u5982\u8ba9\u7528\u6237\u53cd\u590d\u8f93\u5165\u76f4\u5230\u8f93\u5165\u6b63\u786e\u3001\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u76f4\u5230\u7528\u6237\u9009\u62e9\u9000\u51fa\u3002\u5b83\u662f\u5199\u300c\u4ea4\u4e92\u5f0f\u7a0b\u5e8f\u300d\u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>while<\/code> \u7684\u6267\u884c\u6d41\u7a0b\u662f\uff1a\u2460 \u5224\u65ad\u6761\u4ef6\uff08\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u7ed3\u679c\u4e3a True \u6216 False\uff09\u2192 \u2461 \u4e3a\u771f\u5219\u6267\u884c\u5faa\u73af\u4f53 \u2192 \u2462 \u6267\u884c\u5b8c\u8df3\u56de\u7b2c \u2460 \u6b65\u91cd\u65b0\u5224\u65ad \u2192 \u2463 \u6761\u4ef6\u4e3a\u5047\uff0c\u8df3\u51fa\u5faa\u73af\u3002\u5173\u952e\u70b9\uff1a<strong>\u5faa\u73af\u4f53\u91cc\u5fc5\u987b\u6709\u80fd\u5f71\u54cd\u6761\u4ef6\u7684\u4ee3\u7801<\/strong>\uff08\u6bd4\u5982\u8ba1\u6570\u53d8\u91cf\u52a0 1\u3001\u91cd\u65b0\u8bfb\u4e00\u6b21\u8f93\u5165\uff09\uff0c\u5426\u5219\u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f\uff0c\u7a0b\u5e8f\u5c31\u9677\u5165\u6b7b\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>while<\/code> \u50cf<strong>\u5546\u573a\u95e8\u53e3\u7684\u95e8\u536b<\/strong>\uff1a\u53ea\u8ba4\u4e00\u6761\u89c4\u5219\u2014\u2014\u300c\u8fdb\u5e97\u6761\u4ef6\u300d\u6210\u7acb\u5c31\u653e\u4eba\u8fdb\u53bb\uff0c\u8fdb\u53bb\u4e00\u8d9f\u51fa\u6765\u518d\u68c0\u67e5\u4e00\u6b21\uff0c\u6761\u4ef6\u4e0d\u6210\u7acb\u5c31\u5173\u95e8\u4e0d\u653e\u4e86\u3002\u5982\u679c\u4f60\u5fd8\u4e86\u544a\u8bc9\u95e8\u536b\u300c\u4ec0\u4e48\u65f6\u5019\u8be5\u5173\u95e8\u300d\uff08\u5faa\u73af\u4f53\u91cc\u6ca1\u6709\u8ba9\u6761\u4ef6\u53d8\u5047\u7684\u4ee3\u7801\uff09\uff0c\u4ed6\u5c31\u4f1a\u4e00\u76f4\u653e\u4eba\uff0c\u6c38\u8fdc\u4e0d\u505c\u2014\u2014\u8fd9\u5c31\u662f\u6b7b\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>while \u5b9e\u73b0\u300c\u7d2f\u52a0\u8f93\u5165\u7684\u6570\u5b57\uff0c\u76f4\u5230\u8f93\u5165 0 \u7ed3\u675f\u300d<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total = 0\nnum = int(input(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165 0 \u7ed3\u675f\uff09\uff1a\"))\nwhile num != 0:\n    total = total + num\n    num = int(input(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165 0 \u7ed3\u675f\uff09\uff1a\"))\nprint(\"\u603b\u548c\u662f\uff1a\", total)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff08\u793a\u4f8b\u8f93\u5165 5\u30017\u30010\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165 0 \u7ed3\u675f\uff09\uff1a5\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165 0 \u7ed3\u675f\uff09\uff1a7\n\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570\u5b57\uff08\u8f93\u5165 0 \u7ed3\u675f\uff09\uff1a0\n\u603b\u548c\u662f\uff1a 12\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8ba9\u6761\u4ef6\u53d8\u5047\u7684\u4ee3\u7801\uff08\u91cd\u65b0 input\uff09\u5199\u5728\u5faa\u73af\u4f53\u91cc<\/strong>\uff0c\u5982\u679c\u6f0f\u5199\uff0cnum \u6c38\u8fdc\u4e0d\u53d8\uff0c\u5c31\u4f1a\u6b7b\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>while \u8ba1\u7b97\u9636\u4e58<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 5\nresult = 1\ni = 1\nwhile i &lt;= n:\n    result = result * i\n    i = i + 1\nprint(\"5 \u7684\u9636\u4e58 =\", result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5 \u7684\u9636\u4e58 = 120\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">result \u521d\u59cb\u4e3a 1\uff08\u4e58\u6cd5\u7684\u300c\u8d77\u70b9\u300d\uff09\uff1bi \u4ece 1 \u6570\u5230 5\uff0c\u6bcf\u8f6e\u628a i \u4e58\u8fdb result\uff0c\u7136\u540e <code>i = i + 1<\/code>\u3002\u8fd9\u91cc\u7684 <code>i = i + 1<\/code> \u5c31\u662f\u300c\u8ba9\u6761\u4ef6\u53d8\u5047\u300d\u7684\u5173\u952e\u4ee3\u7801\uff0c\u6f0f\u6389\u5c31\u662f\u6b7b\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>\u6b7b\u5faa\u73af\u957f\u4ec0\u4e48\u6837\uff08\u9519\u8bef\u793a\u8303\uff0c\u5343\u4e07\u4e0d\u8981\u8fd0\u884c\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># count = 1\n# while count &lt;= 5:\n#     print(\"\u6211\u505c\u4e0d\u4e0b\u6765\u4e86\")\n# \u4e0a\u9762\u8fd9\u6bb5\u6ca1\u6709 count = count + 1\uff0c\u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f\uff0c\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u7ed3\u675f\u3002\n# \u5982\u679c\u4e0d\u5c0f\u5fc3\u8fd0\u884c\u4e86\u6b7b\u5faa\u73af\u7a0b\u5e8f\uff0c\u5728\u7ec8\u7aef\u6309 Ctrl + C \u5f3a\u5236\u7ec8\u6b62\u3002\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5faa\u73af\u4f53\u91cc\u5fd8\u4e86\u66f4\u65b0\u8ba1\u6570\u53d8\u91cf\uff08<code>i = i + 1<\/code>\uff09 \u2192 \u95ee\u9898\uff1a\u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f\uff0c\u7a0b\u5e8f\u5361\u6b7b\uff08\u6b7b\u5faa\u73af\uff09 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u68c0\u67e5\u5faa\u73af\u4f53\u91cc\u6709\u6ca1\u6709\u300c\u8ba9\u6761\u4ef6\u53d8\u5047\u300d\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u6761\u4ef6\u65b9\u5411\u5199\u53cd\uff08<code>while i > 5<\/code> \u60f3\u6570\u5230 5\uff09 \u2192 \u95ee\u9898\uff1a\u4e00\u5f00\u59cb\u5c31\u662f\u5047\uff0c\u5faa\u73af\u4f53\u4e00\u6b21\u90fd\u4e0d\u6267\u884c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u60f3\u300c\u4ece 1 \u6570\u5230 5\u300d\u5199 <code>while i &lt;= 5<\/code>\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>while True:<\/code> \u5fd8\u8bb0\u5199 break \u2192 \u95ee\u9898\uff1a\u65e0\u9650\u5faa\u73af\uff0c\u7a0b\u5e8f\u6c38\u8fdc\u4e0d\u9000\u51fa \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528 <code>while True:<\/code> \u65f6\u5faa\u73af\u4f53\u5185\u5fc5\u987b\u6709 <code>break<\/code> \u51fa\u53e3\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5faa\u73af\u4f53\u7f29\u8fdb\u9519\u8bef\uff08\u9876\u683c\u5199\uff09 \u2192 \u95ee\u9898\uff1a\u62a5 <code>IndentationError<\/code>\uff0c\u6216\u5faa\u73af\u4f53\u6839\u672c\u6ca1\u88ab\u6267\u884c \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5faa\u73af\u4f53\u5fc5\u987b\u5728 <code>while<\/code> \u884c\u7684\u4e0b\u4e00\u5c42\u7f29\u8fdb\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u6761\u4ef6\u4e3a\u771f\u4e00\u76f4\u8dd1\uff0c\u4f53\u5185\u6539\u6761\u4ef6\uff0c\u522b\u8ba9\u5b83\u6b7b\u5faa\u73af\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 break \/ continue \/ \u5faa\u73af\u7684 else \u2014\u2014 \u63a7\u5236\u5faa\u73af\u7684\u8282\u594f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>break<\/code> \u662f\u300c\u6025\u5239\u8f66\u300d\uff1a\u7acb\u5373\u7ed3\u675f<strong>\u6574\u4e2a<\/strong>\u5faa\u73af\uff1b<code>continue<\/code> \u662f\u300c\u8df3\u8fc7\u8fd9\u4e00\u8f6e\u300d\uff1a\u7ed3\u675f\u672c\u6b21\u5faa\u73af\u4f53\u7684\u6267\u884c\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u3002\u518d\u914d\u5408\u5faa\u73af\u7684 <code>else<\/code>\uff08\u5faa\u73af<strong>\u81ea\u7136\u7ed3\u675f<\/strong>\u624d\u6267\u884c\uff09\uff0c\u53ef\u4ee5\u5199\u51fa\u6e05\u6670\u9ad8\u6548\u7684\u300c\u641c\u7d22 \/ \u8fc7\u6ee4 \/ \u6821\u9a8c\u300d\u903b\u8f91\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e95\u5c42\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a0b\u5e8f\u91cc\u6240\u6709\u300c\u91cd\u590d\u300d\u90fd\u7531\u8df3\u8f6c\u6307\u4ee4\u5b9e\u73b0\uff1a<code>break<\/code> \u7ffb\u8bd1\u6210\u673a\u5668\u6307\u4ee4\u5c31\u662f\u300c\u65e0\u6761\u4ef6\u8df3\u8f6c\u5230\u5faa\u73af\u7ed3\u675f\u4e4b\u540e\u7684\u4f4d\u7f6e\u300d\uff1b<code>continue<\/code> \u662f\u300c\u8df3\u56de\u5faa\u73af\u5f00\u5934\uff08\u518d\u6b21\u5224\u65ad\u6761\u4ef6\u6216\u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\uff09\u300d\u3002\u5faa\u73af\u7684 <code>else<\/code> \u5757\u662f\u4e00\u4e2a\u300c\u786e\u8ba4\u4fe1\u53f7\u300d\uff1a\u53ea\u6709\u5faa\u73af<strong>\u6ca1\u6709\u88ab break \u6253\u65ad<\/strong>\u3001\u81ea\u7136\u7ed3\u675f\u65f6\u624d\u6267\u884c\uff0c\u76f8\u5f53\u4e8e\u300c\u6574\u8f6e\u90fd\u6ca1\u6709\u88ab\u4e2d\u65ad\u300d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6d3b\u7c7b\u6bd4<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f00\u8f66\u4e0a\u73ed\u7684\u8def\u4e0a\uff1a<code>break<\/code> \u662f<strong>\u6025\u5239\u8f66<\/strong>\u2014\u2014\u9047\u5230\u4e8b\u6545\u7acb\u523b\u628a\u8f66\u505c\u4f4f\uff0c\u5269\u4e0b\u7684\u8def\u7a0b\u5168\u90fd\u4e0d\u8d70\u4e86\uff1b<code>continue<\/code> \u662f<strong>\u8df3\u8fc7\u8fd9\u4e00\u7ad9<\/strong>\u2014\u2014\u8fd9\u4e00\u7ad9\u4e0d\u4e0b\u8f66\uff0c\u8f66\u7ee7\u7eed\u5f80\u524d\u5f00\uff1b\u5faa\u73af\u7684 <code>else<\/code> \u5219\u662f\u300c\u8dd1\u5b8c\u5168\u7a0b\u624d\u53d1\u7684\u4e00\u5f20&#8217;\u5230\u7ad9\u786e\u8ba4\u5361&#8217;\u300d\u2014\u2014\u4e2d\u9014\u6025\u5239\u8f66\uff08break\uff09\u4e86\uff0c\u5361\u5c31\u53d1\u4e0d\u51fa\u6765\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>break\u2014\u2014\u627e\u5230\u76ee\u6807\u7acb\u523b\u505c\uff08\u641c\u7d22\u573a\u666f\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#91;3, 8, 15, 22, 31]   # \u5217\u8868 Day 6 \u7ec6\u8bb2\uff0c\u4eca\u5929\u5148\u4f1a\u7528\nfor n in numbers:\n    if n &gt; 20:\n        print(\"\u627e\u5230\u7b2c\u4e00\u4e2a\u5927\u4e8e 20 \u7684\u6570\uff1a\", n)\n        break\n    print(\"\u68c0\u67e5\uff1a\", n)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u68c0\u67e5\uff1a 3\n\u68c0\u67e5\uff1a 8\n\u68c0\u67e5\uff1a 15\n\u627e\u5230\u7b2c\u4e00\u4e2a\u5927\u4e8e 20 \u7684\u6570\uff1a 22\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53 n = 22 \u65f6\u6761\u4ef6\u6210\u7acb\uff0c<code>break<\/code> \u7acb\u523b\u8df3\u51fa\u5faa\u73af\u2014\u2014\u540e\u9762\u7684 31 \u4e0d\u4f1a\u518d\u88ab\u68c0\u67e5\u3002\u8fd9\u662f\u300c\u641c\u7d22\u5230\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5c31\u505c\u300d\u7684\u6807\u51c6\u5199\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>continue\u2014\u2014\u8df3\u8fc7\u4e0d\u9700\u8981\u5904\u7406\u7684\uff08\u8fc7\u6ee4\u573a\u666f\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 7):\n    if i == 3:\n        continue          # 3 \u6211\u4eec\u4e0d\u5904\u7406\uff0c\u76f4\u63a5\u8df3\u8fc7\u672c\u6b21\n    print(\"\u5904\u7406\uff1a\", i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5904\u7406\uff1a 1\n\u5904\u7406\uff1a 2\n\u5904\u7406\uff1a 4\n\u5904\u7406\uff1a 5\n\u5904\u7406\uff1a 6\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>continue<\/code> \u8ba9\u7a0b\u5e8f\u8df3\u8fc7\u672c\u6b21\u5faa\u73af\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u6ce8\u610f continue \u53ea\u8df3\u8fc7\u300c\u8fd9\u4e00\u6b21\u300d\uff0c<strong>\u4e0d\u662f<\/strong>\u7ed3\u675f\u6574\u4e2a\u5faa\u73af\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3 <strong>for-else\u2014\u2014\u5faa\u73af\u6b63\u5e38\u7ed3\u675f\u624d\u6267\u884c else\uff08\u6821\u9a8c\u573a\u666f\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(2, 10):\n    if i % 7 == 0:\n        print(\"\u627e\u5230\u4e86\u80fd\u88ab 7 \u6574\u9664\u7684\u6570\uff1a\", i)\n        break\nelse:\n    print(\"2 \u5230 9 \u4e4b\u95f4\u6ca1\u6709\u80fd\u88ab 7 \u6574\u9664\u7684\u6570\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u627e\u5230\u4e86\u80fd\u88ab 7 \u6574\u9664\u7684\u6570\uff1a 7\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u7684 <code>else<\/code> \u4e0e <code>for<\/code> \u5bf9\u9f50\uff08\u7f29\u8fdb\u5c42\u7ea7\u76f8\u540c\uff09\uff0c<strong>\u4e0d\u662f<\/strong>\u548c if \u5bf9\u9f50\u3002\u627e\u5230\u4e86 7\uff0c\u6267\u884c print \u540e break \u6253\u65ad\u5faa\u73af\uff0celse \u4e0d\u6267\u884c\u3002\u5982\u679c\u628a <code>range(2, 10)<\/code> \u6539\u6210 <code>range(2, 7)<\/code>\uff0c\u627e\u4e0d\u5230 7 \u7684\u500d\u6570\uff0c\u5faa\u73af\u81ea\u7136\u7ed3\u675f\uff0c\u5c31\u4f1a\u6253\u5370 else \u7684\u5185\u5bb9\u2014\u2014\u4f60\u53ef\u4ee5\u4eb2\u624b\u8bd5\u8bd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>continue<\/code> \u5199\u5728 print \u7684\u540e\u9762 \u2192 \u95ee\u9898\uff1a\u5148\u6253\u5370\u4e86\u4e0d\u8be5\u6253\u5370\u7684\u5185\u5bb9\uff0ccontinue \u6ca1\u8d77\u4f5c\u7528 \u2192 \u6b63\u786e\u5199\u6cd5\uff1acontinue \u5fc5\u987b\u5199\u5728\u300c\u60f3\u8df3\u8fc7\u7684\u4ee3\u7801\u300d\u4e4b\u524d\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>break<\/code> \u5199\u5728 if \u5916\u9762 \u2192 \u95ee\u9898\uff1a\u7b2c\u4e00\u6b21\u5faa\u73af\u5c31\u76f4\u63a5\u9000\u51fa\uff0c\u5faa\u73af\u5f62\u540c\u865a\u8bbe \u2192 \u6b63\u786e\u5199\u6cd5\uff1abreak \u5fc5\u987b\u5199\u5728\u6761\u4ef6\u5224\u65ad\uff08if\uff09\u91cc\u9762\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u628a <code>else<\/code> \u548c <code>if<\/code> \u5bf9\u9f50 \u2192 \u95ee\u9898\uff1aelse \u53d8\u6210 if \u7684\u5206\u652f\uff0c\u903b\u8f91\u5b8c\u5168\u53d8\u4e86 \u2192 \u6b63\u786e\u5199\u6cd5\uff1aelse \u5fc5\u987b\u548c for \/ while \u540c\u7f29\u8fdb\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u60f3\u7528 break \u8df3\u51fa\u591a\u5c42\u5d4c\u5957\u5faa\u73af \u2192 \u95ee\u9898\uff1a\u53ea\u80fd\u8df3\u51fa break \u6240\u5728\u7684\u90a3\u4e00\u5c42\uff0c\u5916\u5c42\u8fd8\u5728\u8dd1 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u7528\u300c\u6807\u5fd7\u53d8\u91cf\u300d\u8bb0\u5f55\u72b6\u6001\uff0c\u6216\u628a\u903b\u8f91\u5199\u6210\u51fd\u6570\uff08Day 8\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1abreak \u4e00\u54cd\u5c31\u6536\u5de5\uff0ccontinue \u8df3\u8fc7\u4e0b\u4e00\u8f6e\uff1belse \u7b49\u5faa\u73af\u81ea\u7136\u5b8c\uff0c\u88ab break \u6253\u65ad\u5b83\u4e0d\u5e72\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5 \u5faa\u73af\u5d4c\u5957 \u2014\u2014 \u5faa\u73af\u91cc\u5957\u5faa\u73af<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u91cc\u9762\u518d\u5199\u4e00\u4e2a\u5faa\u73af\uff0c\u5c31\u662f<strong>\u5faa\u73af\u5d4c\u5957<\/strong>\u3002\u5916\u5c42\u5faa\u73af\u6bcf\u6267\u884c\u4e00\u6b21\uff0c\u5185\u5c42\u5faa\u73af\u5c31\u8981\u5b8c\u6574\u6267\u884c\u4e00\u904d\u3002\u5e38\u7528\u4e8e\u751f\u6210\u8868\u683c\u3001\u56fe\u5f62\u3001\u4e58\u6cd5\u53e3\u8bc0\u8868\u8fd9\u7c7b\u300c\u884c \u00d7 \u5217\u300d\u7684\u7ed3\u6784\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f\u4e8b\u9879\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1 <strong>\u5916\u5c42\u63a7\u5236\u884c\uff0c\u5185\u5c42\u63a7\u5236\u5217<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 4):          # \u5916\u5c42\uff1a3 \u884c\n    for j in range(1, 4):      # \u5185\u5c42\uff1a\u6bcf\u884c 3 \u5217\n        print(i, j, end=\" | \")\n    print()                    # \u6bcf\u884c\u7ed3\u675f\u6362\u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 1 | 1 2 | 1 3 |\n2 1 | 2 2 | 2 3 |\n3 1 | 3 2 | 3 3 |\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2 <strong>\u7528 <code>*<\/code> \u753b\u4e00\u4e2a\u4e09\u89d2\u5f62<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 6):\n    print(\"*\" * i)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*\n**\n***\n****\n*****\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6267\u884c\u987a\u5e8f<\/strong>\uff1a\u5916\u5c42\u53d6 i=1\uff0c\u5185\u5c42\u628a j=1,2,3 \u5168\u90e8\u8dd1\u5b8c\uff1b\u7136\u540e\u5916\u5c42\u53d6 i=2\uff0c\u5185\u5c42\u518d\u5168\u90e8\u8dd1\u4e00\u904d\u2026\u2026\u6240\u4ee5\u5185\u5c42\u603b\u5171\u6267\u884c\u7684\u6b21\u6570 = \u5916\u5c42\u6b21\u6570 \u00d7 \u5185\u5c42\u6b21\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6613\u9519\u70b9<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u5199\u6cd5\uff1a\u5fd8\u8bb0\u5185\u5c42\u5faa\u73af\u4e5f\u8981\u7f29\u8fdb \u2192 \u95ee\u9898\uff1a\u62a5 <code>IndentationError<\/code>\uff0c\u6216\u5185\u5c42\u5faa\u73af\u6839\u672c\u6ca1\u751f\u6548 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5185\u5c42\u5faa\u73af\u4f53\u6bd4\u5916\u5c42\u518d\u7f29\u8fdb\u4e00\u5c42\u3002<\/li>\n\n\n\n<li>\u9519\u8bef\u5199\u6cd5\uff1a<code>print()<\/code> \u6362\u884c\u5199\u5728\u5faa\u73af\u5916\u9762 \u2192 \u95ee\u9898\uff1a\u6240\u6709\u5185\u5bb9\u6324\u5728\u4e00\u884c\uff0c\u770b\u4e0d\u51fa\u300c\u884c \u00d7 \u5217\u300d\u7ed3\u6784 \u2192 \u6b63\u786e\u5199\u6cd5\uff1a\u5185\u5c42\u5faa\u73af\u7ed3\u675f\u540e\u7528 <code>print()<\/code> \u6362\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8bb0\u5fc6\u53e3\u8bc0<\/strong>\uff1a\u5916\u5c42\u8d70\u4e00\u6b65\uff0c\u5185\u5c42\u8dd1\u4e00\u5708\uff1b\u603b\u6b21\u6570\u76f8\u4e58\uff0c\u522b\u5fd8\u7f29\u8fdb\u6df1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6 \u52a8\u624b\u5b9e\u8df5\uff1a\u7d2f\u52a0\u5668 + \u4e58\u6cd5\u53e3\u8bc0\u8868<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4efb\u52a1 1\uff1a\u7d2f\u52a0\u5668<\/strong> \u2014\u2014 \u628a 1 \u5230 100 \u7684\u6240\u6709\u6574\u6570\u52a0\u8d77\u6765\uff08\u7b54\u6848\u662f 5050\uff09\uff0c\u5206\u522b\u7528 for \u548c while \u5404\u5199\u4e00\u904d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7d2f\u52a0\u5668\uff1a\u7528 for \u5b9e\u73b0\ntotal = 0\nfor i in range(1, 101):\n    total = total + i\nprint(\"1+2+...+100 =\", total)\n# \u8f93\u51fa\uff1a1+2+...+100 = 5050\n\n# \u7d2f\u52a0\u5668\uff1a\u7528 while \u5b9e\u73b0\ntotal = 0\ni = 1\nwhile i &lt;= 100:\n    total = total + i\n    i = i + 1\nprint(\"while \u7248\u672c\u7ed3\u679c\uff1a\", total)\n# \u8f93\u51fa\uff1awhile \u7248\u672c\u7ed3\u679c\uff1a 5050\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4efb\u52a1 2\uff1a\u4e58\u6cd5\u53e3\u8bc0\u8868<\/strong> \u2014\u2014 \u7528\u5faa\u73af\u5d4c\u5957\u6253\u5370\u4e5d\u4e5d\u4e58\u6cd5\u53e3\u8bc0\u8868\uff0c\u683c\u5f0f\u4e3a <code>1x1=1<\/code> \u8fd9\u79cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4e5d\u4e5d\u4e58\u6cd5\u53e3\u8bc0\u8868\nfor i in range(1, 10):          # \u5916\u5c42\uff1a\u4e58\u6570 1~9\n    for j in range(1, i + 1):   # \u5185\u5c42\uff1a\u53ea\u5230 i\uff0c\u907f\u514d\u91cd\u590d\uff081x2 \u548c 2x1 \u53ea\u7559\u4e00\u4e2a\uff09\n        print(f\"{j}x{i}={i * j}\", end=\"  \")\n    print()                     # \u6bcf\u884c\u7ed3\u675f\u6362\u884c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1x1=1\n1x2=2  2x2=4\n1x3=3  2x3=6  3x3=9\n1x4=4  2x4=8  3x4=12  4x4=16\n1x5=5  2x5=10  3x5=15  4x5=20  5x5=25\n1x6=6  2x6=12  3x6=18  4x6=24  5x6=30  6x6=36\n1x7=7  2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49\n1x8=8  2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64\n1x9=9  2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u60f3\u4e00\u60f3<\/strong>\uff1a\u4e3a\u4ec0\u4e48\u5185\u5c42\u7528 <code>range(1, i + 1)<\/code>\uff1f\u8bd5\u7740\u6539\u6210 <code>range(1, 10)<\/code> \u8fd0\u884c\u770b\u770b\uff0c\u4f53\u4f1a\u300c\u907f\u514d\u91cd\u590d\u300d\u7684\u8bbe\u8ba1\u601d\u8def\u3002<code>f\"{j}x{i}={i * j}\"<\/code> \u662f f-string\uff0c\u82b1\u62ec\u53f7\u91cc\u7684\u53d8\u91cf\u4f1a\u88ab\u66ff\u6362\u6210\u5b83\u7684\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4eca\u65e5\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5faa\u73af\u662f\u4ec0\u4e48\uff1a\u8ba9\u7a0b\u5e8f\u81ea\u52a8\u91cd\u590d\u6267\u884c\u540c\u4e00\u6bb5\u4ee3\u7801\uff0c\u7f29\u8fdb\u7684\u4ee3\u7801\u5757\u5c31\u662f\u5faa\u73af\u4f53\n\u5faa\u73af\u7684\u672c\u8d28\uff1aCPU \u7684\u6307\u4ee4\u6307\u9488\u6267\u884c\u5b8c\u5faa\u73af\u4f53\u540e\u300c\u8df3\u56de\u300d\u5faa\u73af\u5f00\u5934\uff0c\u76f4\u5230\u6761\u4ef6\u4e0d\u6210\u7acb\u6216\u5143\u7d20\u53d6\u5b8c\n<code>for<\/code> \u9002\u5408\u300c\u77e5\u9053\u6b21\u6570 \/ \u904d\u5386\u96c6\u5408\u300d\uff0c<code>while<\/code> \u9002\u5408\u300c\u6761\u4ef6\u63a7\u5236\u3001\u6b21\u6570\u672a\u77e5\u300d\n<code>range()<\/code> \u542b\u5934\u4e0d\u542b\u5c3e\uff1a<code>range(n)<\/code> \u751f\u6210 0~n-1\uff0c\u6b65\u957f\u4e3a\u8d1f\u53ef\u4ee5\u5012\u7740\u6570\n<code>while<\/code> \u5faa\u73af\u4f53\u91cc\u5fc5\u987b\u6709\u8ba9\u6761\u4ef6\u53d8\u5047\u7684\u4ee3\u7801\uff0c\u9632\u6b62\u6b7b\u5faa\u73af\uff1b<code>Ctrl + C<\/code> \u53ef\u5f3a\u5236\u7ec8\u6b62\n<code>break<\/code> \u7ed3\u675f\u6574\u4e2a\u5faa\u73af\uff0c<code>continue<\/code> \u8df3\u8fc7\u672c\u6b21\uff0c\u5faa\u73af\u7684 <code>else<\/code> \u53ea\u5728\u81ea\u7136\u7ed3\u675f\u65f6\u6267\u884c\n\u5faa\u73af\u5d4c\u5957\uff1a\u5916\u5c42\u6bcf\u6267\u884c\u4e00\u6b21\uff0c\u5185\u5c42\u5b8c\u6574\u6267\u884c\u4e00\u904d\uff0c\u603b\u6b21\u6570 = \u5916\u5c42\u6b21\u6570 \u00d7 \u5185\u5c42\u6b21\u6570\n\u5b8c\u6210\u7d2f\u52a0\u5668\uff08\u7ed3\u679c 5050\uff09\u548c\u4e58\u6cd5\u53e3\u8bc0\u8868<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u62a5\u9519<\/th><th>\u539f\u56e0<\/th><th>\u4fee\u590d<\/th><\/tr><\/thead><tbody><tr><td><code>SyntaxError: expected ':'<\/code><\/td><td>for \/ while \u884c\u5c3e\u5fd8\u4e86\u5199\u5192\u53f7<\/td><td>\u68c0\u67e5\u662f\u4e0d\u662f <code>for i in range(5):<\/code> \u5c11\u4e86\u5192\u53f7<\/td><\/tr><tr><td><code>IndentationError: expected an indented block<\/code><\/td><td>\u5faa\u73af\u4f53\u5fd8\u4e86\u7f29\u8fdb<\/td><td>\u5faa\u73af\u4f53\u7edf\u4e00\u7f29\u8fdb 4 \u4e2a\u7a7a\u683c<\/td><\/tr><tr><td>\u7a0b\u5e8f\u5361\u4f4f\u4e0d\u52a8\uff08\u6b7b\u5faa\u73af\uff09<\/td><td>while \u7684\u6761\u4ef6\u6c38\u8fdc\u4e3a\u771f\uff0c\u6ca1\u6709\u8ba9\u6761\u4ef6\u53d8\u5047\u7684\u4ee3\u7801<\/td><td>\u68c0\u67e5\u5faa\u73af\u4f53\u91cc\u662f\u5426\u66f4\u65b0\u4e86\u6761\u4ef6\u53d8\u91cf\uff1b\u4e34\u65f6\u6309 Ctrl+C \u7ec8\u6b62<\/td><\/tr><tr><td><code>IndexError<\/code> \u6216 <code>NameError<\/code><\/td><td>\u5faa\u73af\u53d8\u91cf\u540d\u5199\u9519\u6216\u8d8a\u754c\u8bbf\u95ee<\/td><td>\u68c0\u67e5\u5faa\u73af\u53d8\u91cf\u540d\u3001<code>range<\/code> \u7684\u8303\u56f4<\/td><\/tr><tr><td><code>TypeError: 'int' object is not iterable<\/code><\/td><td>\u628a\u6570\u5b57\u76f4\u63a5\u653e\u8fdb\u4e86 for\uff08<code>for i in 5:<\/code>\uff09<\/td><td>\u6570\u5b57\u4e0d\u80fd\u904d\u5386\uff0c\u8981\u5199\u6210 <code>for i in range(5):<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1 \u5faa\u73af\u7684\u672c\u8d28 \u2014\u2014 \u628a\u91cd\u590d\u52b3\u52a8\u4ea4\u7ed9\u7a0b\u5e8f \u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u5b66\uff1f \u5faa\u73af\uff08loop\uff09\u662f\u8ba9\u7a0b\u5e8f\u53cd\u590d\u6267\u884c\u540c\u4e00\u6bb5\u4ee3\u7801\u7684\u8bed\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[16,9],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-python","category-code-dev"],"_links":{"self":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":2,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ltrainyard.icu\/index.php\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}