3.5 · 迭代 (for 与 while)
目标: 用
for计数、用while等条件。记住嵌套循环是 2C 内容。
for —— 计数循环
python
for i in range(5):
print(i) # 0, 1, 2, 3, 41
2
2
range(start, stop, step):
| 调用 | 序列 |
|---|---|
range(5) | 0, 1, 2, 3, 4 |
range(1, 6) | 1, 2, 3, 4, 5 |
range(0, 10, 2) | 0, 2, 4, 6, 8 |
range(10, 0, -1) | 10, 9, 8, …, 1 |
遍历列表
python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)1
2
3
2
3
enumerate 同时取索引 + 值:
python
for i, fruit in enumerate(fruits):
print(i, fruit)1
2
2
while —— 条件循环
python
count = 0
while count < 5:
print(count)
count += 11
2
3
4
2
3
4
事先不知道要循环几次时用 while。
哨兵控制 while
python
total = 0
n = int(input("Number (0 to stop): "))
while n != 0:
total += n
n = int(input("Number (0 to stop): "))
print("Total:", total)1
2
3
4
5
6
2
3
4
5
6
用户输 0 停。0 是哨兵值。
break 与 continue
| 关键字 | 效果 |
|---|---|
break | 立刻退出循环 |
continue | 跳过本轮余下,去下轮 |
python
for i in range(10):
if i == 5:
break # 5 时停
print(i) # 0 1 2 3 4
for i in range(10):
if i % 2 == 0:
continue # 跳偶数
print(i) # 1 3 5 7 91
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
避免死循环
python
# 坏 —— count 永不变
count = 0
while count < 10:
print(count)
# 好
count = 0
while count < 10:
print(count)
count += 11
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
实例 · 1..n 求和
python
n = int(input("n? "))
total = 0
for i in range(1, n + 1):
total += i
print("Sum:", total)1
2
3
4
5
2
3
4
5
实例 · 猜数字
python
import random
secret = random.randint(1, 100)
guess = -1
while guess != secret:
guess = int(input("Guess: "))
if guess < secret:
print("Too low")
elif guess > secret:
print("Too high")
print("Correct!")1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
学生常见错误
range偏差 1:range(1, 10)给 1..9,不是 1..10。while忘更新控制变量 → 死循环。- 必修伪代码写嵌套循环(属 2C)。
考试式题目
题(5 分): 写 Python 程序读数字直到用户输入 -1,然后输出所输入数字的平均(不计 -1)。若无输入,输出「no input」。
参考答案:
python
total = 0
count = 0
while True:
n = int(input("Number (-1 to stop): "))
if n == -1:
break
total += n
count += 1
if count == 0:
print("no input")
else:
print("Average:", total / count)1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
关键要点
for计数;while「直到条件」。range(start, stop, step)不含stop。break退出;continue跳过。- 必修部分避免嵌套循环。
➡️ 下一节:3.6 列表(一维数组)