While loops
Python Basics Lesson 8 3:00 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A for loop is perfect when you know the count in advance: print one to five, five passes, done.
当你事先知道次数时,for 循环再合适不过:打印 1 到 5,五轮,结束。
But now think about asking someone a question until they type yes.
但现在想一想:一直问某个人问题,直到他输入 yes 为止。
How many times is that?
那是多少次?
You do not know, and neither does the program — there is no number to write inside range.
你不知道,程序也不知道——range 里面根本没有数字可以写。
So Python offers a second kind of loop, one that repeats on a condition instead of a count.
所以 Python 提供了第二种循环:它按条件重复,而不是按次数。
Here is the whole machine.
整台机器就是这样。
The loop tests first: is count greater than zero?
循环先判断:count 是不是大于 0?
If that is true, it runs the body underneath.
如果为真,就执行下面的循环体。
Then — and this is the part people forget — it goes back to the test and asks again.
然后——这一步很多人会忘—— 它回到判断处再问一次。
Round and round until the answer is false, and only then does it carry on past the loop.
就这样一圈一圈,直到答案为假,才继续往循环后面走。
Because the test comes first, a while loop can run zero times: if count starts at zero, the body never runs at all.
因为判断在前,while 循环可能一次都不执行:如果 count 一开始就是 0, 循环体根本不会运行。
That returning arrow is also the danger.
那条返回的箭头同时也是危险所在。
Look at this loop: it prints count, goes back, tests again — but nothing in the body ever changes count, so the answer is true forever and the program never stops.
看这个循环:它打印 count,返回,再判断—— 但循环体里没有任何东西改变 count,所以答案永远为真,程序永远停不下来。
One more line fixes it: take one away each pass.
加一行就解决了:每一轮减去 1。
Now the value walks down towards zero and the test eventually fails.
这样这个值就一步步走向 0,判断最终会失败。
Every while loop you write needs that line; if one ever runs away from you, press Stop.
你写的每一个 while 循环都需要这样一行;万一哪次真的失控了,就按 Stop。
Two words give you finer control.
有两个词能给你更细的控制。
Break stops the loop immediately, wherever you are in the body.
break 会立刻停止循环,不管你在循环体的哪个位置。
Continue skips the rest of this pass and goes straight back to the test.
continue 跳过本轮剩下的部分,直接回到判断。
Together with while true — a condition that is always true — break gives you the exam's repeat-until shape: run the body, and leave when something inside says so.
再配上 while True——一个永远为真的条件——break 就给了你考试里的 REPEAT…UNTIL 结构:先执行循环体,等里面某个条件说停时再离开。
The third task in this lesson is exactly that.
这一课的第三题正是这样。
Now the lesson's first task: a countdown from five.
现在做课程里的第一道题:从 5 开始倒数。
Start at five, before the loop.
在循环之前把 count 设成 5。
The test asks whether count is greater than zero.
判断问的是 count 是不是大于 0。
The body prints the number and then takes one away — print first, subtract second, or you would print four at the top.
循环体先打印这个数,然后减去 1——先打印,后相减, 否则一开头打印出来的就会是 4。
Watch it run: five, four, three, two, one.
看它运行:5、4、3、2、1。
When count reaches zero the test is false, the loop ends, and nothing more prints.
当 count 变成 0 时判断为假,循环结束,不再打印任何东西。
Four things to take with you.
带走四点。
One: a while loop repeats on a condition, not a count, so reach for it when the number of passes is unknown.
第一:while 循环按条件重复,而不是按次数, 所以在轮数未知时就用它。
Two: it tests first, so it may run zero times.
第二:它先判断,因此可能一次都不执行。
Three: change something inside the body, or it never ends.
第三:循环体里一定要改变某个东西,否则它永远不会结束。
Four: while true plus break is how you write repeat-until in Python.
第四:while True 加 break,就是在 Python 里写 REPEAT…UNTIL 的方法。
Now do the three tasks; the third one hunts for the first square above fifty.
现在去做那三道题;第三题要找出第一个大于 50 的平方数。