Skip to content

The loop that never ends · ⁨停不下来的循环⁩

English

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 下面的行属于循环、会重复;退回到最左边的行在循环之后,只在最后执行一次。这里循环永远走不到它的最后一行,所以它的问题永远成立。页面会发现并替你停下来,但程序应该自己停。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩