---
# System prepended metadata

title: wk05_迴圈
tags: [Python]

---

### wk05_1005_B1003217

### [inclass practice]

<pre>
ch04_迴圈
    4.1 range 函式
        認識 range 函式
        range 函式的語法
    4.2 for 迴圈
        巢狀 for 迴圈
        break 命令
        continue 命令
    4.3 while 迴圈
</pre>


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

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


```python
print(" i love you"*50)
```

     i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you
    


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

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


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

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


```python
c1="amanda"
c2="alice"
c3="amma"
c4="amy"

c=["amanda","alice","amma","amy"]

print(type(c))

print(c1,c2,c3,c4)

print(c*10)
```

    <class 'list'>
    amanda alice amma amy
    ['amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy', 'amanda', 'alice', 'amma', 'amy']
    


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

for i in [1,2,3,4,5,6,7,8,9]:
    print("$*"*i)
    
for i in [1,2,3,4,5,6,7,8,9]:
    print("$*")
```

    *
    **
    ***
    ****
    *****
    $*
    $*$*
    $*$*$*
    $*$*$*$*
    $*$*$*$*$*
    $*$*$*$*$*$*
    $*$*$*$*$*$*$*
    $*$*$*$*$*$*$*$*
    $*$*$*$*$*$*$*$*$*
    $*
    $*
    $*
    $*
    $*
    $*
    $*
    $*
    $*
    


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

    $
    $$
    $$$
    $$$$
    $$$$$
    $$$$$$
    $$$$$$$
    $$$$$$$$
    $$$$$$$$$
    $$$$$$$$$$
    


```python
n=10

ttl=1
ttl=ttl+2
ttl=ttl+3
ttl=ttl+4
ttl=ttl+5
ttl=ttl+6
ttl=ttl+7
ttl=ttl+8
ttl=ttl+9
ttl=ttl+10

print(ttl)
```

    55
    


```python
n=10
ttl=0

for i in range(1,100):
    ttl=i+ttl
    
print(ttl)
```

    4950
    


```python
n=10
ttl=0

for i in range(1,11,2): #只加單數
    ttl=i+ttl
    
print(ttl)
```

    25
    


```python
n=10
ttl=0

for i in range(0,11,2): #只加雙數
    ttl=i+ttl
    
print(ttl)
```

    30
    


```python
n=int(input("entery your n "))

# n=10

ttl=0

for i in range(1,n+1,2):
    ttl=i+ttl
    
print(ttl)
```

    entery your n 10
    25
    


```python
n=10

ttl=0

for i in range(1,11):
    if(i%3==0 or i%5==0): 
        ttl=i+ttl
    
print(ttl)
```

    33
    


```python
n=int(input("輸入一變數n "))
a=int(input("除a "))
b=int(input("除b "))

ttl=0

for i in range(1,n):
    if(i%a==0 or i%b==0): 
        ttl=i+ttl
    
print(ttl)
```

    輸入一變數n 11
    除a 3
    除b 5
    33
    


```python
a=2
b=9

for i in range(1,b+1):
    print(a,"*",i,"=",a*i)
    

#print("2*1=2")
#print("2*2=4")
```

    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
    


```python
a=2
b=9

for i in range(1,b+1):
    print(a,"*",i,"=",a*i)
    print(i,a,b)
```

    2 * 1 = 2
    1 2 9
    2 * 2 = 4
    2 2 9
    2 * 3 = 6
    3 2 9
    2 * 4 = 8
    4 2 9
    2 * 5 = 10
    5 2 9
    2 * 6 = 12
    6 2 9
    2 * 7 = 14
    7 2 9
    2 * 8 = 16
    8 2 9
    2 * 9 = 18
    9 2 9
    


```python
a=9
b=9

for j in range(2,b+1):
    print("這是",j,"的乘法")
    for i in range(1,b+1):
        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
    


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




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



數列變數=range(起始值,終止值,間隔值)

### [afterclass practice]

#### 綜合演練 選擇題1-10

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

B 3.執行下列程式，結束迴圈後，n的值為多少?
    for n in range(1,5,2):
        print(n,end="")
    print("結束迴圈後 n=",n)
  (A)1
  (B)3
  (C)5
  (D)7
    
A 4.執行for迴圈時，如果想要提前離開迴圈，應使用何種指令?
  (A)break
  (B)return
  (C)exit
  (D)pause
    
C 5.執行下列程式，下列結果何者正確?
    list1=range(5,-1,-2)
    print(list(list1))
  (A)[5,-1,-2]
  (B)[5,4,3,2,1,0,-1]
  (C)[5,3,1]
  (D)[5,3,1,-1]
  
B 6.while迴圈若一開始測試條件就不成立，則while內程式區塊將會如何處理?
  (A)執行一次
  (B)一次都不執行
  (C)重複執行
  (D)編輯錯誤
  
D 7.下列哪一個指令可再迴圈中跳果後面的敘述值接回到迴圈的開頭?
  (A)exit
  (B)return
  (C)pause
  (D)continue
  
C 8.執行下列程式，結束迴圈後，sum的值為多少?
    sum=0
    n=8
    for i in range(1,n+1,2):
        sum +=i
    print(sum)
  (A)8
  (B)9
  (C)16
  (D)28
  
D 9.執行下列程式，可看到多少個「#」字?
    n=5
    for i in range(1,n):
        for j in range(1,i+1):
            print("#",end="")
    print()
  (A)5
  (B)6
  (C)7
  (D)10

C 10.執行下列程式，結束迴圈後，total的值為多少?
    total=i=1
    n=5
    while(i<=n):
        total *=i
        i+=1
    print(total)
  (A)5
  (B)24
  (C)120
  (D)720
  
  </pre>


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

    [0, 1, 2, 3, 4]
    


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

    [6, 4, 2]
    


```python
#3

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

    13結束迴圈後 n= 3
    


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

    [5, 3, 1]
    


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

    16
    


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

    #
    ##
    ###
    ####
    


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

    120
    

#### 教學影音 lesson 9

<pre>
for 迴圈用於執行固定次數的動作
    語法
        for 變數 in 串列:
            程式區塊 
         ex:計算1+2+...+10之和
            sum=0
            for i in range(1,11):
                sum +=i
            print("sum=%d" sum)
            
while 迴圈用於執行次數不固定的迴圈
    語法
        while條件式:
            成式區塊
        ex:計算1+2+...+10之和
            total=n=0
            while(n<10)
                n +=1
                total +=n
            print(total)
            # 1+2+...+10=55
            
break強制離開迴圈，例如:
    for i in range(1,11):
        if(i==4):
            break
        print(i,end=",") #執行結果:1,2,3

continue在迴圈執行中途暫停不往下執行，而跳到迴圈起始處繼續執行，例如:
    for i in range(1,11):
        if(i==4):
            continue
        print(i,end=",")
        #執行結果:1,2,3,5,6,7,8,9,10
</pre>

### [self practice]

實作4 :
小杰在for迴圈中使用or運算 if( i % 3 ==0  or i % 7 ==0)，
求出數值 1~100中，所有是3或7倍數的數之總和 


```python
n=10

ttl=0

for i in range(1,100):
    if(i%3==0 or i%7==0): 
        ttl=i+ttl
    
print(ttl)
```

    2208
    
