The loop that never ends · 停不下来的循环
The loop that never ends
Run this first and watch what happens. The line that makes count smaller is there — but it is outside the loop, so it never runs.
In Python the indentation is the structure: lines indented under while belong to the loop and repeat, a line back at the left margin comes after the loop and runs once at the end. Here the loop never reaches its last line, so its question stays true for ever. The page will notice and stop it for you, but your program should stop itself.
停不下来的循环
先运行一下看看会发生什么。让 count 变小的那一行是有的 —— 但它在循环外面,所以永远不会执行。
在 Python 里,缩进就是结构:缩进在 while 下面的行属于循环、会重复;退回到最左边的行在循环之后,只在最后执行一次。这里循环永远走不到它的最后一行,所以它的问题永远成立。页面会发现并替你停下来,但程序应该自己停。
Run this first and watch what happens. The line that makes count smaller is there — but it is outside the loop, so it never runs. Move it inside so the program prints 5, 4, 3, 2, 1 and stops. · 先运行一下看看会发生什么。让 count 变小的那一行是有的 —— 但它在循环外面,所以永远不会执行。把它移到循环里面,让程序打印 5、4、3、2、1 然后停下。
Click Run to see the output here. · 点击“运行”查看此处输出。