## 迴圈 #### 2026資訊之芽Python班 ##### 曾子齊 --- ## 迴圈是什麼? ---- 試想情況:從0輸出到99 ```python= print(0) print(1) print(2) print(3) print(4) print(5) print(6) print(7) # ...... ``` 沒完沒了啊! ---- 或是要表達對資芽的愛 ```python= print("我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽我愛資芽") ``` 很明顯不是一個妥當的方法。 ---- 這時候就需要迴圈! 迴圈就是 **可以幫你做重複性的事情的工具** --- ## whlie迴圈 ---- 大致上長這樣: ```python= while condition: # do something ``` 當條件condition成立,執行縮排內的程式 每次執行完檢查一次condition,如果還是True就再執行一次 ---- 例子:輸出1~99 ```python= i = 1 while i < 100: print(i) i += 1 ``` 當 ($i$小於100)時, 執行 (輸出$i$ 並把$i$的值+1) ---- ![image](https://hackmd.io/_uploads/H17GxKHube.png) ---- 再一個例子:印出一條長度為n的康莊大道 ![image](https://hackmd.io/_uploads/HyCOp6KuWx.png) ---- 先將輸入存在變數n中,並且印出康莊大道的最前排 ```python= n = int(input()) # 將輸入以整數形式存在變數n中 print("-------") # 輸出康莊大道的第一排 ``` ---- 接下來我需要重複n次 ```python print("| |") ``` 這時候就需要迴圈出馬! ---- ```python= n = int(input()) # 將輸入以整數形式存在變數n中 print("-------") # 輸出康莊大道的第一排 # 請出迴圈 while ??? : pass ``` ---- 發現需要有一個變數幫我計算已經跑了幾圈 ```python= n = int(input()) print("-------") counter = 0 # 負責計算迴圈跑了幾圈 while ??? : # do something counter += 1 # 每次執行完一次迴圈,就把執行次數加一 ``` ---- 每次迴圈執行輸出康莊大道 ```python= n = int(input()) print("-------") counter = 0 while ??? : print("| |") # 每次迴圈輸出康莊大道! counter += 1 ``` ---- 迴圈不能一直執行下去,要告訴電腦什麼情況下才繼續執行 ```python= n = int(input()) print("-------") counter = 0 while counter < n : # 當迴圈數還小於n的時候,繼續執行~ print("| |") counter += 1 ``` ---- 最後為康莊大道收個尾 ```python= n = int(input()) print("-------") counter = 0 while counter < n : print("| |") counter += 1 print("-------") # 收尾! ``` 注意因為沒有縮排,所以它屬於在迴圈外面,不會重複被執行。 ---- 如果條件永遠都是True的話...... ```python= while True: print("我將狂奔至宇宙的盡頭!") ``` 這就是無限迴圈(Infinite Loop) 遇到這種狀況,請手動停止程式。 ---- 停止程式的方法 - 在終端機輸入Ctrl+C - 在VS Code按下紅色正方形 ![image](https://hackmd.io/_uploads/r1h0zKHObx.png) 在OJ(Online Judge)上會因為執行時間過久得到TLE(Time Limit Exceed) ---- [幫助你理解的GIF](https://www.pinterest.com/pin/280208408042574206/) --- ## 引導練習:印出九九乘法表 ---- 印出九九乘法表聽起來好難,那簡化一下 首先練習印出第一行九九乘法表吧! ```python= print("1*1=1\t", end="") print("1*2=2\t", end="") print("1*3=3\t", end="") ...... ``` 可以用一個變數代替變動的部分: ```python= print("1*", j, "=", 1*j, "\t", end="", sep="") ``` ---- 把它放到迴圈裡,並且在j<10的情況下重複執行 ```python= j = 1 while j < 10 : print("1*", j, "=", 1*j, "\t", end="", sep="") j += 1 ``` 得到結果: ![image](https://hackmd.io/_uploads/BkIn8KruZe.png) ---- 這樣就印出以1開頭的這一行了 但是我也想要2~9開頭的 那只好再用一次迴圈了 ---- 讓$i$從1到9,每一次都輸出一行乘法表。 ```python= i = 1 while i < 10 : ''' 輸出以i開頭的一行乘法表 ''' print() # 每次輸出完一行就換行 i += 1 ``` 然後把剛才輸出一行的程式套進去 ---- ```python= i = 1 while i < 10 : # 輸出以i開頭的一行乘法表 j = 1 while j < 10 : print(i, "*", j, "=", i*j, "\t", end="", sep="") j += 1 print() # 每次輸出完一行就換行 i += 1 ``` 就完成了! ![image](https://hackmd.io/_uploads/Hy1gutr_-l.png) --- 自己練習 [683. 3n+1問題](https://tioj.sprout.tw/contests/40/problems/683) [729. 卍九九乘法改乂](https://tioj.sprout.tw/contests/40/problems/729) --- ## for迴圈 ---- 大致上長這樣: ```python= for i in 1, 2, 3, 4, 5: # do something ``` 宣告一個變數(這個例子裡是$i$),讓他在每次迴圈中變成不同數字。 ---- 例子:輸出1~99 ```python= for i in range(1, 100): # 1 ~ 99 print(i) ``` ---- range函數:回傳可迭代物件 range(start, end, step) start預設0、step預設1 ```python= range(5) # 0, 1, 2, 3, 4 range(2, 6) # 2, 3, 4, 5 range(0, 10, 2) # 0, 2, 4, 6, 8 ``` ---- 補充: 可迭代物件是什麼? 可以暫時想像成「可以一個一個數的東西」 以後教的陣列或字串也是可迭代物件 ---- for迴圈會在每一次迴圈中 將變數設為可迭代物件的每一項 ```python= for i in range(5): print(i) # 0, 1, 2, 3 ,4 # 補充,感興趣的可以自己跑跑看 for i in [11, 21, 1211, 111221, 312211]: print(i) for i in "banana": print(i, end=" ") ``` ---- 用for迴圈可以簡單的讓程式執行數次 ```python= # 讚嘆100次資芽 for _ in range(100): print("感恩資芽,讚嘆資芽!") ``` 其中如果不會使用到變數,通常命名為_以便閱讀 ---- 可以自己想想看如何用for迴圈印出康莊大道:P ##### 或是用slido要求我現場打code --- ## 引導練習:印出九九乘法表 #### 再一次 ---- 論如何印出一行開頭為1的九九乘法表: ```python= for j in range(1, 10): print("1*", j, "=", 1*j, "\t", end="", sep="") ``` ---- 我要印九行: ```python= for i in range(1, 10): # 印一行以i為開頭的乘法表 print() # 換行 ``` ---- 合體: ```python= for i in range(1, 10): for j in range(1, 10): print(i, "*", j, "=", i*j, "\t", end="", sep="") # 記得把1改成i print() # 換行 ``` --- 自己練習 [680. 有點沒梗了,所以來印三角形吧](https://tioj.sprout.tw/contests/40/problems/680) --- ## continue && break ---- 有時候我們因為一些特殊的原因 想要讓跳過某次迴圈,給迴圈偷個懶 或是直接讓迴圈停下來去放個假 迴圈表示很樂意,那要怎麼做呢? ---- ```python= i = 1 while True: # 這不是把我電腦炸掉的無限迴圈嗎?! print("我要狂奔到宇宙盡......") i += 1 if i > 10: break # 喔沒事了 ``` break可以跳出當前所在的迴圈 防止世界毀滅 ---- 九九乘法表,但是我討厭數字4,所以不印第四行 ```python= for i in range(1, 10): # 如果這是第四行,直接進入下一次迴圈 if i==4: continue for j in range(1, 10): print(i, "*", j, "=", i*j, "\t", end="", sep="") print() ``` contiune可以跳過後面的程式,直接到下一圈迴圈 --- ## 一些黑魔法 ---- ### while-else while接上一個else,讓迴圈結束時執行else的指令 ```python= i = 1 while i < 10: print(i) i += 1 else: print("迴圈結束") ``` ---- for生成陣列(補充,看不懂沒關係,之後會講) ```python= ls = [i**2 for i in range(6)] print(ls) ``` ![image](https://hackmd.io/_uploads/rk1xUcB_Wx.png) --- ## 最後提醒 ---- - 迴圈一定要有終止條件 - while迴圈執行到條件不成立 - for迴圈執行固定次數或已知的順序 - 放在迴圈內的程式碼記得縮排 --- ## 謝謝大家 ---- 但,是不是少了什麼? ---- ### 回家作業! [793. 用錢錢擺成菱形陣](https://tioj.sprout.tw/contests/40/problems/793) [794. 高級地毯不是這樣的!](https://tioj.sprout.tw/contests/40/problems/794) [797. 數5不比數羊好?!](https://tioj.sprout.tw/contests/40/problems/797) ---- 如果還不夠的話...... ### 挑戰題!☆\*: .。. o(≧▽≦)o .。.:\*☆ [795. 輾轉難眠的夜](https://tioj.sprout.tw/contests/40/problems/795) [798 . 數n真比數羊好!!](https://tioj.sprout.tw/contests/40/problems/798)
{"contributors":"[{\"id\":\"9b0a9794-5c7e-4a79-baad-660057d9ee00\",\"add\":6270,\"del\":493,\"latestUpdatedAt\":1772336892676}]","description":"試想情況:從0輸出到99","title":"2026資訊之芽Python班:迴圈"}
    87 views