While loops · while 循环
Loop while a condition is true
- A
whileloop repeats as long as its condition is true. - It checks the condition before each pass.
- Use it when you do not know the number of repeats in advance.
当条件为真时循环
while循环会只要条件为真就一直重复。- 它在每一轮之前检查条件。
- 当你事先不知道要重复多少次时,就用它。
Always change the condition
- Something inside the loop must move toward making the condition false.
- If it never becomes false, the loop runs forever (an infinite loop).
- Press Stop if a loop ever gets stuck.
一定要改变条件
- 循环里必须有某些东西逐渐让条件变为假。
- 如果条件永远不变假,循环就会一直运行(死循环,infinite loop)。
- 如果循环卡住了,按停止。
count = 3
while count > 0:
print(count)
count = count - 1
break and continue
breakstops the loop right away.continueskips the rest of this pass and goes to the next one.while True:loops forever until abreakends it.
break 和 continue
break会立刻停止循环。continue会跳过本轮剩下的部分,直接进入下一轮。while True:会一直循环,直到某个break结束它。
n = 0
while True:
n = n + 1
if n == 4:
break
print(n)
Checking before or after
- A
whileloop checks first, so it may run zero times. - A "repeat until" loop checks after, so it always runs at least once.
- Python has no
repeat; usewhile True:with abreakat the end.
先检查还是后检查
while循环先检查,所以它可能运行零次。- "重复直到"型循环后检查,所以它至少运行一次。
- Python 没有
repeat;可以用while True:并在末尾加一个break。
In Cambridge pseudocode
whileis the pre-condition loop;REPEAT … UNTILis the post-condition loop.
用剑桥伪代码表示
while是前置条件循环;REPEAT … UNTIL是后置条件循环。
Count ← 3
WHILE Count > 0
OUTPUT Count
Count ← Count - 1
ENDWHILE
REPEAT
OUTPUT Count
Count ← Count + 1
UNTIL Count = 3
Common mistakes
- Change something inside the loop so the condition can become false, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
常见错误
- 在循环里改动某个值,让条件能变为假,否则会无限循环。
while先检查条件,所以它可能一次都不运行。
Now you try
- Make sure your condition will eventually become false.
- Stuck on the loop? Do the short drill Finish the countdown first. It is one missing line.
- Press Check answer to test your code.
A while loop builds up a value · while 循环逐步累加
A while loop repeats until its condition is false. · while 循环一直重复,直到条件为假。
Use a while loop to count down. Each turn, print the value of count, then make count one smaller. Stop when it reaches 0, so the output is 5, 4, 3, 2, 1 — one per line. · 用 while 循环倒数。每一轮先打印 count 的值,再把 count 减小 1。减到 0 就停下,所以输出是 5、4、3、2、1,每个一行。
Click Run to see the output here. · 点击“运行”查看此处输出。
Use a while loop to add up the numbers 1 to · 到 10. Store the total in total (the answer is 55). · 用 while 循环把 1 到 10 的数字加起来。把总和存进 total(答案是 55)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Start · 开始 n at 1 and add 1 each pass. The loop stops too soon: it ends while n is still 1. Change the condition so it keeps going while n * n is at most 50. When it stops, n is the first whole number whose square is over 50. · 从 n = 1 开始,每一轮加 1。这个循环停得太早:n 还是 1 就结束了。把条件改成:只要 n * n 不超过 50 就继续。停下时,n 就是第一个平方大于 50 的整数。
Click Run to see the output here. · 点击“运行”查看此处输出。
Same answer, with break. n starts at 1 inside while True:. The if is already inside the loop and n grows after · 之后 it. Change the condition so you break when n * n is greater than 50. Leave the answer in n (it is 8). Do not add 1 before the test — that makes the answer 9. · 同一个答案,这次用 break。n 从 1 开始,写在 while True: 里面。if 已经在循环里了,n 在它之后才加 1。把条件改成:当 n * n 大于 50 时 break。把答案留在 n 里(它是 8)。不要在判断之前加 1 —— 那样答案会变成 9。
Click Run to see the output here. · 点击“运行”查看此处输出。
Start with n = 1 and steps = 0. While n is smaller than 1000, double n and add 1 to · 到 steps. When the loop ends, n is 1024. How many doublings did it take? Leave the answer in steps. · 从 n = 1、steps = 0 开始。只要 n 小于 1000,就把 n 乘以 2,并让 steps 加 1。循环结束时 n 是 1024。一共翻倍了几次?把答案留在 steps 里。
Click Run to see the output here. · 点击“运行”查看此处输出。