---
# System prepended metadata

title: wk05_1005_ch05迴圈
tags: [python]

---

# wk05_1005_ch05迴圈_B1003205醫放三謝彤

<pre>
上課內容:
range函式=>認識及語法
for迴圈=>巢狀for迴圈、break命令、continue命令
while迴圈

for迴圈用在執行固定次數的迴圈；while用在不固定
</pre>

## [inclass practice]


```python
print("*")
print("**")
print("***")
print("****")
print("*****")
```

    *
    **
    ***
    ****
    *****
    


```python
print("*" *5)
```

    *****
    


```python
print("*"*1)
print("*"*2)
print("*"*3)
print("*"*4)
print("*"*5)
```

    *
    **
    ***
    ****
    *****
    


```python
a="*"
print(a)
```

    *
    


```python
a="*"
print(a*100)
```

    ****************************************************************************************************
    


```python
a="*"
b=5
print(a*b)
```

    *****
    


```python
c1="謝彤"
c2="陳筱涵"
c3="王語蔚"
print(c1,c2,c3)
```

    謝彤 陳筱涵 王語蔚
    


```python
c1="謝彤"
c2="陳筱涵"
c3="王語蔚"
c=[c1,c2,c3] #也可以寫成["謝彤"."陳筱涵","王語蔚"]
print(c) #[]打出來的東西會有單引號
print(type(c)) #c的type是list (因為有[])
```

    ['謝彤', '陳筱涵', '王語蔚']
    <class 'list'>
    


```python
for  i in[1,2,3] : #第一圈i=1；第二圈i=2；第三圈i=3
    print(i)
```

    1
    2
    3
    


```python
print("*"*1)
print("*"*2)
print("*"*3)
print("*"*4)
print("*"*5)
for i in[1,2,3,4,5]:
    print("*"*i)
```

    *
    **
    ***
    ****
    *****
    *
    **
    ***
    ****
    *****
    


```python
for i in[1,2,3,4,5]:
    print("OAO") #每一行都會出現一個OAO
```

    OAO
    OAO
    OAO
    OAO
    OAO
    


```python
range(10)
```




    range(0, 10)




```python
list(range(10))
```




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




```python
list(range(4,10))
```




    [4, 5, 6, 7, 8, 9]




```python
list(range(4,10,2)) #起始值,終點值,間距
```




    [4, 6, 8]




```python
for i in[10]:
    print("*"*i)
```

    **********
    


```python
for i in range(10): #記得是從0開始
    print("*"*i)
```

    
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    


```python
for i in range(1,11):
    print("*"*i)
```

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    


```python
n=10
total=1
total=total+2
total=total+3
total=total+4
total=total+5
total=total+6
total=total+7
total=total+8
total=total+9
total=total+10

print(total)

```

    55
    


```python
n=10
total=0
for i in range(1,11): #表示從1加到10而已 可寫成range(11)
    total=total+i

print(total)
```

    55
    


```python
n=10
total=0
for i in range(1,11,2): #只要單數
    total=total+i

print(total)
```

    25
    


```python
n=10
total=0
for i in range(0,11,2): #只要雙數
    total=total+i

print(total)
```

    30
    


```python
n=int(input("請輸入n值"))
total=0
for i in range(n+1):
    total=total+i

print(total)
```

    請輸入n值10
    55
    


```python
total=0
for i in range(1,10001):
    if i%3==0 or i%5==0 :
        total=total+i
print(total)
```

    23341668
    


```python
n=int(input("請輸入最終值"))
a=int(input("請輸入其中一個整除值"))
b=int(input("請輸入另一個整除值"))
total=0
for i in range(1,n+1):
    if i%a==0 or i%b==0 :
        total=total+i
print(total)
```

    請輸入最終值10000
    請輸入其中一個整除值3
    請輸入另一個整除值5
    23341668
    


```python
a=2
for j in range(2,10):
    print("這是",j,"的乘法")
    for i in range(1,10):
        print(j,"*",i,"=",j*i)

    
    
```

    這是 2 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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 的乘法
    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
    

## [afterclass practice]

### 綜合演練

<pre>
1.執行下列程式，下列結果何者正確?
(A)[1,2,3,4] (B)[1,2,3,4,5] (C)[0,1,2,3,4] (D)[0,1,2,3,4,5]

Answer:(C)


```python
list1= range(5)
print(list(list1))
```

    [0, 1, 2, 3, 4]
    

<pre>
2.執行下列程式，下列結果何者正確?
(A)[6,4,2] (B)[6,0,2] (C)[6,4,2,0] (D)[6,4,2,0,-2]

Answer:(A)


```python
list1=range(6,0,-2)
print(list(list1))
```

    [6, 4, 2]
    

<pre>
3.執行下列程式，結束迴圈後，n的值多少?
(A)1 (B)3 (C)5 (D)7

Answer:(B)


```python
for n in range(1,5,2):
    print(n,end=" ")
print("結束迴圈後n=",n)
```

    1 3 結束迴圈後n= 3
    

<pre>
4.執行for迴圈時，如果想要提前離開迴圈，應使用何種指令?
(A)break (B)return (C)exit (D)pause

Answer:(A)

<pre>
5.執行下列程式，下列結果何者正確?
(A)[5,-1,-2] (B)[5,4,3,2,1,0,-1] (C)[5,3,1] (D)[5,3,1,-1]

Answer:(C)


```python
list1=range(5,-1,-2)
print(list(list1))
```

    [5, 3, 1]
    

<pre>
6.while迴圈若一開始測試條件就不成立，則while內程式區塊將會如何處理?
(A)執行一次 (B)一次都不執行 (C)重複執行 (D)編譯錯誤

Answer:(B)

<pre>
7.下列哪一個指令可在迴圈中跳過後面的敘述直接回到迴圈的開頭?
(A)exit (B)return (C)pause (D)continue

Answer:(D)

<pre>
8.執行下列程式，結束迴圈後，sum的值為多少?
(A)8 (B)9 (C)16 (D)28

Answer:(C)


```python
sum=0
n=8
for i in range(1,n+1,2):
    sum +=i
print(sum)
```

    16
    

<pre>
9.執行下列程式，可看到多少個#字?
(A)5 (B)6 (C)7 (D)10

Answer:(D)


```python
n=5
for i in range(1,n):
    for j in range(1,i+1):
        print("#",end="")
    print()
```

    #
    ##
    ###
    ####
    

<pre>
10.執行下列程式，結束迴圈後，total的值為多少?
(A)5 (B)24 (C)120 (D)720

Answer:(C)


```python
total=i=1
n=5
while(i<=n):
    total*=i
    i+=1
print(total)


```

    120
    

## [self-practice]