Counting loops · 计数循环
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
用 for 重复
for循环会把代码重复固定的次数。- 循环体要缩进,就像
if一样。 - 循环变量在每一轮都会取一个新的值。
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
用 range 计数
range(5)给出数字0, 1, 2, 3, 4(到 5 之前停下)。range(1, 6)给出1, 2, 3, 4, 5(包含起点,不包含终点)。range(0, 10, 2)以 2 为步长计数。
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
累加求和(累加器)
- 累加器(accumulator)是一个在循环里不断增长的变量。
- 在循环之前先把它设为
0。 - 每一轮都往里面加,从而得到累计总和。
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
用剑桥伪代码表示
- Python 的
for … range循环就是考试里的计数循环FOR … NEXT。
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
一行里既有文字又有数字
print里的逗号会在各部分之间加一个空格。print("7 x", 1, "=", 7 * 1)打印出7 x 1 = 7。+不能把数字和文字连在一起。用逗号,或者先用str把数字变成文字。
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
常见错误
range(n)数0到n - 1,不是1到n。range(a, b)在b之前停止。- 循环体必须缩进在
for那行下面。
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
现在轮到你
- 每个任务都要用带
range(...)的for循环。 - 按检查答案来测试你的代码。
A counting loop, pass by pass · 计数循环,一轮一轮看
A for loop repeats its body; watch the counter and the running total · 累加总和. · for 循环重复执行循环体;看计数器和累加的总和。
Use a for loop to print the numbers 1 to · 到 5, each on its own line. · 用 for 循环打印数字 1 到 5,每个数字单独占一行。
Click Run to see the output here. · 点击“运行”查看此处输出。
Use a loop and an accumulator to add up every whole number from 1 to · 到 100. Store the total in total. · 用一个循环和累加器,把从 1 到 100 的每个整数加起来。把总和存进 total。
Click Run to see the output here. · 点击“运行”查看此处输出。
Multiply together every whole number from 1 to · 到 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · 把从 1 到 5 的每个整数相乘(即 1×2×3×4×5)。把 product 从 1 开始,并把结果存进它。
Click Run to see the output here. · 点击“运行”查看此处输出。
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · 只打印一行:7 x 1 = 7。把已经写了一半的 print 补完。各部分之间用逗号,打印时会自动加空格:print("7 x", 1, "=", 7 * 1)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · 用 for 循环打印 7 的乘法表的前五行:7 x 1 = 7、7 x 2 = 14,一直到 7 x 5 = 35。上一题的 print 已经写在循环里了 —— 把那个 1 换成循环变量。
Click Run to see the output here. · 点击“运行”查看此处输出。