### wk06_1012_迴圈
4.2 for 迴圈
- break 命令
- continue 命令
4.3 while 迴圈
#### 【inclass practice】
###### {範例}
<pre>
6. 最小公倍數 <multiple>
7. 顯示正整數數列,排除5的倍數 <except5>
8. while迴圈計算階乘 <while>
</pre>
```python
for i in range(1, 11):
if(i==6):
break
print(i, end =",")
```
1,2,3,4,5,
```python
for i in range(1, 11):
if(i==6):
continue
print(i, end =",")
```
1,2,3,4,5,7,8,9,10,
```python
# n = int(input("enter n"))
n=10
for i in range(1, n+1):
if i % 5 ==0:
continue
print(i)
```
1
2
3
4
6
7
8
9
```python
n = int(input("enter n"))
for i in range(1, n+1):
if i % 5 ==0:
continue
print(i)
```
enter n7
1
2
3
4
6
7
```python
# n = int(input("enter n"))
n = 10000
for i in range(1, n+1):
if i == 9:
break
print(i)
```
1
2
3
4
5
6
7
8
```python
a = 10
b = 7
maxno = a * b
for i in range(1, maxno + 1):
if i % a == 0 and i % b == 0 :
break
print(a, b, i)
```
10 7 70
###### {綜合演練}
<pre>
實作5:
一個正整數除了1和自己外,無法再被其他數整除,這個數就是質數。
請輸入一正整數,列出此數的所有正因數,並判斷使數字是否為質數</pre>
```python
n = 131
p = 1
counter = 0
while p <= n:
if n % p == 0:
counter = counter + 1
p = p + 1
if counter > 2:
ans = "合數"
else :
ans = "質數"
print(n, ans, "有", counter, "因數")
```
131 質數 有 2 因數
```python
p=1
counter=0
n = int(input("請輸入正整數:"))
print(n,"的因數有",end =" ")
while (p<=n):
if (n % p == 0):
print(p,end =" ")
counter += 1
p += 1
print()
if (counter == 2):
print(n,"是質數")
else:
print(n,"不是質數")
```
請輸入正整數:25
25 的因數有 1 5 25
25 不是質數
```python
num = 6
ans = 1
n = 0
while n < num :
n = n + 1
ans =ans * n
print(num,"!=", ans)
```
6 != 720
###### {補充 while 練習}
1. 猜數字遊戲:寫一個程式,隨機選擇一個1到100的數字,然後要求玩家通過輸入猜測該數字,直到猜對為止。使用while迴圈來實現,並在玩家猜對時結束遊戲。
```python
import random
target_number = random.randint(1, 100)
guess = None
attempts = 0
while guess != target_number:
guess = int(input("請猜一個1到100的數字:"))
attempts += 1
if guess < target_number:
print("低了!再猜一次。")
elif guess > target_number:
print("高了!再猜一次。")
print(f"恭喜!你猜對了,答案就是{target_number}。你一共猜了{attempts}次。")
```
請猜一個1到100的數字:50
恭喜!你猜對了,答案就是50。你一共猜了1次。
2. 使用者輸入:寫一個程式,要求使用者輸入一個整數,然後使用while迴圈,只有當輸入的數字是正整數時,才停止請求輸入。如果輸入不是正整數,則繼續要求輸入,直到收到正確的輸入。
```python
user_input = None
while not user_input:
user_input = input("請輸入一個正整數:")
if user_input.isdigit():
user_input = int(user_input)
else:
user_input = None
print("請輸入正確的整數。")
print(f"您輸入了正整數:{user_input}")
```
請輸入一個正整數:2.1
請輸入正確的整數。
請輸入一個正整數:-1
請輸入正確的整數。
請輸入一個正整數:10
您輸入了正整數:10
3. 倒數計時器:寫一個程式,請求使用者輸入一個正整數作為倒數計時的秒數,然後使用while迴圈從輸入的秒數倒數到0。在每個秒數的間隔打印出剩餘的秒數。
```python
import time
seconds = int(input("請輸入倒數計時的秒數:"))
while seconds > 0:
print(f" {seconds}")
time.sleep(1)
seconds -= 1
print("倒計時結束!")
```
請輸入倒數計時的秒數:10
10
9
8
7
6
5
4
3
2
1
倒計時結束!
4. 累積加法:寫一個程式,要求使用者不斷輸入整數,並使用while迴圈計算這些整數的總和。當使用者輸入0時,停止接受輸入並印出總和。
```python
total = 0
while True:
num = int(input("請輸入一個整數(輸入0結束):"))
if num == 0:
break
total += num
print(f"總和:{total}")
```
請輸入一個整數(輸入0結束):3
請輸入一個整數(輸入0結束):5
請輸入一個整數(輸入0結束):6
請輸入一個整數(輸入0結束):17
請輸入一個整數(輸入0結束):0
總和:31
5. 數字猜謎:寫一個程式,在1到10之間選擇一個隨機數字,然後要求玩家通過輸入猜測該數字。使用while迴圈來實現,並提供猜測是太高還是太低的提示,直到猜對為止。
```python
import random
# 生成隨機數字
target_number = random.randint(1, 10)
guess = None
while guess != target_number:
guess = int(input("請猜一個1到10的數字:"))
if guess < target_number:
print("太低了!再猜一次。")
elif guess > target_number:
print("太高了!再猜一次。")
print(f"恭喜!你猜對了,答案是{target_number}。")
```
請猜一個1到10的數字:5
太高了!再猜一次。
請猜一個1到10的數字:3
太低了!再猜一次。
請猜一個1到10的數字:4
恭喜!你猜對了,答案是4。
#### 【afterclass practice】
1. 複習ch03、ch04
2. 教學影音 lesson 7、9