Loops — Python 练习题
Loops repeat code while a condition is true (while) or iterate over a sequence (for). They eliminate repetitive code and are essential for processing collections and running simulations.
Python for loop: for i in range(5) runs i = 0,1,2,3,4. range(start, stop) excludes stop. while loops need a condition that eventually becomes false to avoid infinite loops.
Common patterns: accumulator (total += x), counter (count += 1), and finding max/min. Always check loop bounds and off-by-one errors.
练习的技能
- Writing for and while loops
- Using range() correctly
- Tracing loop output
- Avoiding infinite loops
练习题:Loops
说明: 仔细解答每一道题。清晰地展示所有解题过程。在指定位置或按指示在另一张纸上写出最终答案。
-
1.Write a
forloop that prints the numbers from 1 to 10, each on a new line.
Loops — 练习题(续)
-
2.What is the output of the following code?
total = 0 for i in range(5): total += i print(total)
Loops — 练习题(续)
-
3.Write a
whileloop that repeatedly asks the user to enter a number. The loop should stop when the user enters the number 0. For each number entered (except 0), print its square.
Loops — 练习题(续)
-
4.How many times will the following loop execute?
count = 0 while count < 10: print("Hello") count += 2
Loops — 练习题(续)
-
5.Use a
forloop to calculate and print the sum of all even numbers between 1 and 20 (inclusive).
Loops — 练习题(续)
-
6.What is the output of this code?
for i in range(3, 0, -1): print(i)
Loops — 练习题(续)
-
7.Write a loop that prints the multiplication table for the number 7 (from 1 to 10). Each line should show the multiplication and result, e.g., “7 x 1 = 7”.
Loops — 练习题(续)
-
8.Which of the following loops will run infinitely? (Circle all that apply)
- A.
- B.
- C.
- D.
Loops — 练习题(续)
-
9.Write a
forloop that iterates over a listnames = ["Alice", "Bob", "Charlie"]and prints each name in uppercase.
Loops — 练习题(续)
-
10.The following code is supposed to print the numbers 1 through 5, but it has a bug. Identify the bug and write the corrected code.
num = 1 while num <= 5: print(num)
参考答案
-
1.
最终答案:
for i in range(1, 11): print(i) -
2.
The loop sums 0 + 1 + 2 + 3 + 4 = 10.最终答案: 10
-
3.
最终答案:
num = int(input("Enter a number: ")) while num != 0: print(num ** 2) num = int(input("Enter a number: ")) -
4.
The loop executes 5 times (count = 0, 2, 4, 6, 8).最终答案: 5
参考答案(续)
-
5.
最终答案:
total = 0 for num in range(2, 21, 2): total += num print(total)The sum is 110. -
6.
最终答案:
3 2 1
-
7.
最终答案:
for i in range(1, 11): print(f"7 x {i} = {7 * i}")
参考答案(续)
-
8.
最终答案: Options (a) and (d) will run infinitely.
-
9.
最终答案:
names = ["Alice", "Bob", "Charlie"] for name in names: print(name.upper()) -
10.
最终答案: The bug is that
numis never incremented, so the loop runs infinitely. Corrected code:num = 1 while num <= 5: print(num) num += 1
需要避免的常见错误
- Off-by-one errors with range boundaries
- Infinite while loops when condition never changes
- Modifying loop variable incorrectly
- Using for when while is clearer for unknown iterations
生成您自己的练习表
想要一套难度自选的 loops 新题?使用 WorksheetSmith 在几秒内编译一份可打印的自定义 PDF。
最后更新:2026