)` 并在表达式返回 `True` 时执行下方缩进的代码区块。不然,“退出”while循环并跳过其缩进代码。\n",
"- 如果缩进的代码块被执行了,回到第一步。\n",
"\n",
"让我们观察一个例子:\n",
"```python\n",
"# 演示基本的while循环\n",
"total = 0\n",
"while total < 2:\n",
" total += 1 # 和 `total = total + 1` 等值\n",
"\n",
"print(total) # `total` 现在的值为 2\n",
"```\n",
"\n",
"此代码会执行以下步骤:\n",
"\n",
"1. 定义变量 `total`,并为其赋值 `0`\n",
"2. 运算 `0 < 2`,其返回 `True`:进入循环主体的代码块\n",
"3. 执行代码块:为 `total` 赋值 `0 + 1`\n",
"4. 运算 `1 < 2`,其返回 `True`:进入循环主体的代码块\n",
"5. 执行代码块:为 `total` 赋值 `1 + 1`\n",
"6. 运算 `2 < 2`,其返回 `False`:*跳过*循环主体的代码块\n",
"7. 打印 `total` 的值(2)\n",
"\n",
"注意,如果我们一开始定义 `total = 3`,那条件表达式 `3 < 2` 会直接返回 `False`,且缩进的代码块将一次都不会执行。\n",
"\n",
"\n",
"\n",
"**警告!**\n",
"\n",
"你可能(一不小心)编写了条件语句永远为真的while循环。这会导致你的代码无限运行!如果这在Jupyter记事本中意外发生,打断或重启你的壳(kernel)。\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"**阅读理解:简单while循环**\n",
"\n",
"设一列表 `x`,其成员皆为的非零正数。在列表的结尾附加列表所有值的和。重复进行这一步,直到 `x` 的最后一个成员的值至少是100。使用一个while循环。\n",
"\n",
"如果你定义 `x = [1]`,那在你的while循环结束后 `x` 应该是 `[1, 1, 2, 4, 8, 16, 32, 64, 128]`。\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 循环中的 `break`,`continue`,和 `else` 句段\n",
"你可以在for循环和while循环的主体中使用 `continue` 和 `break` 语句。它们分别提供一些额外的“短路”和提前退出循环的方式。\n",
"\n",
"在循环中遇到 `break` 会导致程序立刻退出循环:\n",
"\n",
"```python\n",
"# 提前退出循环\n",
">>> for item in [1, 2, 3, 4, 5]:\n",
"... if item == 3:\n",
"... print(item, \" ...break!\")\n",
"... break\n",
"... print(item, \" ...next iteration\")\n",
"```\n",
"```\n",
"1 ...next iteration\n",
"2 ...next iteration\n",
"3 ...break!\n",
"```\n",
"\n",
"你可以在任何循环结尾添加一个 `else` 句段。此else语句的主体*只有在循环自然退出(没有被 `break` 语句打断)后才会执行*。\n",
"\n",
"```python\n",
"# 在循环结尾添加一个else句段\n",
">>> for item in [2, 4, 6]:\n",
"... if item == 3:\n",
"... print(item, \" ...break!\")\n",
"... break\n",
"... print(item, \" ...next iteration\")\n",
"... else:\n",
"... print(\"if you are reading this, then the loop completed without a 'break'\")\n",
"```\n",
"```\n",
"2 ...next iteration\n",
"4 ...next iteration\n",
"6 ...next iteration\n",
"if you are reading this, then the loop completed without a 'break'\n",
"```\n",
"\n",
"在循环中遇到 `continue` 语句会导致程序立刻回到检查条件语句这一步。\n",
"```python\n",
"# 演示循环中的 `continue` 语句\n",
">>> x = 1\n",
">>> while x < 4:\n",
"... print(\"x = \", x, \">> enter loop-body <<\")\n",
"... if x == 2:\n",
"... print(\"x = \", x, \" continue...back to the top of the loop!\")\n",
"... x += 1\n",
"... continue\n",
"... x += 1\n",
"... print(\"--reached end of loop-body--\")\n",
"```\n",
"```\n",
"x = 1 >> enter loop-body <<\n",
"--reached end of loop-body--\n",
"x = 2 >> enter loop-body <<\n",
"x = 2 continue...back to the top of the loop!\n",
"x = 3 >> enter loop-body <<\n",
"--reached end of loop-body--\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"**阅读理解:控制循环中的流**\n",
"\n",
"重复循环一个整数列表,求其中偶数的和并将此和加到一个总值中。重复此过程,直到总值超过100,或已经循环这个列表超过50次。在总值大于等于100时打印它。\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 官方说明文档链接\n",
"\n",
"- [“for”语句](https://docs.python.org/3/reference/compound_stmts.html#the-for-statement)\n",
"- [“while”语句](https://docs.python.org/3/reference/compound_stmts.html#the-while-statement)\n",
"- [“break”,“continue”,和“else”句段](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops)\n",
"- [“pass”语句](https://docs.python.org/3/tutorial/controlflow.html#pass-statements)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 阅读理解答案"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**简单for循环:解**\n",
"```python\n",
"for letter in \"abcdefghij\":\n",
" if letter in \"aeiou\":\n",
" print(letter)\n",
"```\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**简单while循环:解**\n",
"```python\n",
"while x[-1] < 100:\n",
" x.append(sum(x))\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**控制循环中的流:解**\n",
"\n",
"```python\n",
"x = [3, 4, 1, 2, 8, 10, -3, 0]\n",
"num_loop = 0\n",
"total = 0\n",
"\n",
"while total < 100:\n",
" for item in x:\n",
" # 当 `item` 是奇数时立刻跳到\n",
" # 下一轮for循环\n",
" if item % 2 == 1:\n",
" continue\n",
" else:\n",
" total += item\n",
" num_loop += 1\n",
"\n",
" # 只有在循环了50次之后才\n",
" # 打断while循环中\n",
" if 50 < num_loop:\n",
" break\n",
"else:\n",
" print(total)\n",
"```\n"
]
}
],
"metadata": {
"jupytext": {
"text_representation": {
"extension": ".md",
"format_name": "markdown",
"format_version": "1.2",
"jupytext_version": "1.3.0rc2"
}
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}