---
# System prepended metadata

title: wk06_1012_while loop
tags: [python]

---

###### tags: `python`
## wk06_1012 迴圈 while , break, continue

1. break
2. continue
3. while

## 【inclass practice】


```python
#6. 最小公倍數 \<multiple>
#a = int(input("請輸入 a 的值："))
#b = int(input("請輸入 b 的值："))
a = 5 *19
b = 17
maxno = a * b
for i in range(1, maxno + 1):
    if(i % a == 0 and i % b == 0):
        break
print("%d 和 %d 的最小公倍數=%d"  % (a, b, i))  
```

    95 和 17 的最小公倍數=1615
    


```python
# 7. 顯示正整數數列，排除5的倍數 \<except5>

#n = int(input("請輸入正整數："))
n = 6
for i in range(1, n+1):
    if i % 5 ==0:
        continue
    print(i,end=" ")
```

    1 2 3 4 6 


```python
p=1
counter=0
#n = int(input("請輸入正整數："))
n = 10
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,"不是質數", "因數有", counter, "個")
```

    10 的因數有 1 2 5 10 
    10 不是質數 因數有 4 個
    


```python
#1. 觀察各種不同參數的以range()建立的數列 \<range>
list1=range(10)
list2=range(1,10)
list3=range(1,10,2)
list4=range(10,1,-2)
print(list(list1))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(list2))  # [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(list(list3))  # [1, 3, 5, 7, 9]
print(list(list4))  # [10, 8, 6, 4, 2]
```

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    [1, 3, 5, 7, 9]
    [10, 8, 6, 4, 2]
    


```python
# 5. 九九乘法表 <ninenine>
for i in range(1,10):
    for j in range(1,10):
        product = i * j
        #print("%d*%d=%2d   " % (i, j, product), end="")
        print(i, "*", j , "=", i * j)
    print()
```

    1 * 1 = 1
    1 * 2 = 2
    1 * 3 = 3
    1 * 4 = 4
    1 * 5 = 5
    1 * 6 = 6
    1 * 7 = 7
    1 * 8 = 8
    1 * 9 = 9
    
    2 * 1 = 2
    2 * 2 = 4
    2 * 3 = 6
    2 * 4 = 8
    2 * 5 = 10
    2 * 6 = 12
    2 * 7 = 14
    2 * 8 = 16
    2 * 9 = 18
    
    3 * 1 = 3
    3 * 2 = 6
    3 * 3 = 9
    3 * 4 = 12
    3 * 5 = 15
    3 * 6 = 18
    3 * 7 = 21
    3 * 8 = 24
    3 * 9 = 27
    
    4 * 1 = 4
    4 * 2 = 8
    4 * 3 = 12
    4 * 4 = 16
    4 * 5 = 20
    4 * 6 = 24
    4 * 7 = 28
    4 * 8 = 32
    4 * 9 = 36
    
    5 * 1 = 5
    5 * 2 = 10
    5 * 3 = 15
    5 * 4 = 20
    5 * 5 = 25
    5 * 6 = 30
    5 * 7 = 35
    5 * 8 = 40
    5 * 9 = 45
    
    6 * 1 = 6
    6 * 2 = 12
    6 * 3 = 18
    6 * 4 = 24
    6 * 5 = 30
    6 * 6 = 36
    6 * 7 = 42
    6 * 8 = 48
    6 * 9 = 54
    
    7 * 1 = 7
    7 * 2 = 14
    7 * 3 = 21
    7 * 4 = 28
    7 * 5 = 35
    7 * 6 = 42
    7 * 7 = 49
    7 * 8 = 56
    7 * 9 = 63
    
    8 * 1 = 8
    8 * 2 = 16
    8 * 3 = 24
    8 * 4 = 32
    8 * 5 = 40
    8 * 6 = 48
    8 * 7 = 56
    8 * 8 = 64
    8 * 9 = 72
    
    9 * 1 = 9
    9 * 2 = 18
    9 * 3 = 27
    9 * 4 = 36
    9 * 5 = 45
    9 * 6 = 54
    9 * 7 = 63
    9 * 8 = 72
    9 * 9 = 81
    
    


```python
# 5. 九九乘法表 <ninenine>
for i in range(1,10):
    for j in range(1,10):
        product = i * j
        print("%d*%d=%2d   " % (i, j, product), end="")
    print()
```

    1*1= 1   1*2= 2   1*3= 3   1*4= 4   1*5= 5   1*6= 6   1*7= 7   1*8= 8   1*9= 9   
    2*1= 2   2*2= 4   2*3= 6   2*4= 8   2*5=10   2*6=12   2*7=14   2*8=16   2*9=18   
    3*1= 3   3*2= 6   3*3= 9   3*4=12   3*5=15   3*6=18   3*7=21   3*8=24   3*9=27   
    4*1= 4   4*2= 8   4*3=12   4*4=16   4*5=20   4*6=24   4*7=28   4*8=32   4*9=36   
    5*1= 5   5*2=10   5*3=15   5*4=20   5*5=25   5*6=30   5*7=35   5*8=40   5*9=45   
    6*1= 6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   6*7=42   6*8=48   6*9=54   
    7*1= 7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   7*8=56   7*9=63   
    8*1= 8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   8*9=72   
    9*1= 9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   
    


```python
# 5. 九九乘法表 <ninenine>
for i in range(1,10):
    for j in range(1,10):
        product = i * j
        print("%d*%d=%2d   " % (j, i, product), end="")
    print()
```

    1*1= 1   2*1= 2   3*1= 3   4*1= 4   5*1= 5   6*1= 6   7*1= 7   8*1= 8   9*1= 9   
    1*2= 2   2*2= 4   3*2= 6   4*2= 8   5*2=10   6*2=12   7*2=14   8*2=16   9*2=18   
    1*3= 3   2*3= 6   3*3= 9   4*3=12   5*3=15   6*3=18   7*3=21   8*3=24   9*3=27   
    1*4= 4   2*4= 8   3*4=12   4*4=16   5*4=20   6*4=24   7*4=28   8*4=32   9*4=36   
    1*5= 5   2*5=10   3*5=15   4*5=20   5*5=25   6*5=30   7*5=35   8*5=40   9*5=45   
    1*6= 6   2*6=12   3*6=18   4*6=24   5*6=30   6*6=36   7*6=42   8*6=48   9*6=54   
    1*7= 7   2*7=14   3*7=21   4*7=28   5*7=35   6*7=42   7*7=49   8*7=56   9*7=63   
    1*8= 8   2*8=16   3*8=24   4*8=32   5*8=40   6*8=48   7*8=56   8*8=64   9*8=72   
    1*9= 9   2*9=18   3*9=27   4*9=36   5*9=45   6*9=54   7*9=63   8*9=72   9*9=81   
    


```python

```


```python

```

### 補充 while 
1. 猜數字遊戲：寫一個程式，隨機選擇一個1到100的數字，然後要求玩家通過輸入猜測該數字，直到猜對為止。使用while迴圈來實現，並在玩家猜對時結束遊戲。

2. 使用者輸入：寫一個程式，要求使用者輸入一個整數，然後使用while迴圈，只有當輸入的數字是正整數時，才繼續請求輸入。如果輸入不是正整數，則繼續要求輸入，直到收到正確的輸入。

3. 倒數計時器：寫一個程式，請求使用者輸入一個正整數作為倒數計時的秒數，然後使用while迴圈從輸入的秒數倒數到0。在每個秒數的間隔打印出剩餘的秒數。

4. 累積加法：寫一個程式，要求使用者不斷輸入整數，並使用while迴圈計算這些整數的總和。當使用者輸入0時，停止接受輸入並印出總和。

5. 數字猜謎：寫一個程式，在1到10之間選擇一個隨機數字，然後要求玩家通過輸入猜測該數字。使用while迴圈來實現，並提供猜測是太高還是太低的提示，直到猜對為止。



```python
import time
#countdown = int(input("請輸入倒數的秒數："))
countdown = 3
while countdown > 0:              
    print(countdown)
    time.sleep(1)
    countdown -= 1

print("時間到！")
```

    3
    2
    1
    時間到！
    


```python
total = 0

while True:
    #num = int(input("請輸入一個整數（輸入0結束）："))
    num=0
    if num == 0:
        break
    total += num

print(f"總和：{total}")
```

    總和：0
    


```python
user_input = None

while not user_input:
    #user_input = input("請輸入一個正整數：")
    user_input = 10
    if user_input.isdigit():
        user_input = int(user_input)
    else:
        user_input = None
        print("請輸入正確的整數。")

print(f"您輸入了正整數：{user_input}")
```


    ---------------------------------------------------------------------------

    AttributeError                            Traceback (most recent call last)

    <ipython-input-10-7f2f1f5f6786> in <module>
          4     #user_input = input("請輸入一個正整數：")
          5     user_input = 10
    ----> 6     if user_input.isdigit():
          7         user_input = int(user_input)
          8     else:
    

    AttributeError: 'int' object has no attribute 'isdigit'



```python
import random

# 生成隨機數字
#target_number = random.randint(1, 100)
target_number = 100
guess = None
attempts = 0

while guess != target_number:
    #guess = int(input("請猜一個1到100的數字："))
    guess = 100
    attempts += 1

    if guess < target_number:
        print("太低了！再猜一次。")
    elif guess > target_number:
        print("太高了！再猜一次。")

print(f"恭喜！你猜對了，答案是{target_number}。你總共猜了{attempts}次。")
```


```python
import random

# 生成隨機數字
#target_number = random.randint(1, 10)
target_number = 10
guess = None

while guess != target_number:
    #guess = int(input("請猜一個1到10的數字："))
    guess = 10
    if guess < target_number:
        print("太低了！再猜一次。")
    elif guess > target_number:
        print("太高了！再猜一次。")

print(f"恭喜！你猜對了，答案是{target_number}。")
```

## 【afterclass practice】

1. 複習ch03、ch04 
2. 教學影音 lesson 7、9