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 列表(一維數組)