# 複習一下 ---- ## 計算長方體的體積 ---- ### 請試著用輸入(input)完成以下內容 output ```python 輸入你的長=5 輸入你的寬=5 輸入你的高=5 你的圓柱體體積為:125 ``` --- # 控制流程 ---- ![image alt](https://cdn.discordapp.com/attachments/678292614398869513/1033372921068003448/unknown.png) ---- # 簡單講就是if啦! ---- ## 舉個例子好了: ```python if 我的身高>=180cm: 我就倒立吃屎!(認真) ``` --- # 你還可以用else代表否定式(否則)的結果 ---- ## 簡單講就是這樣~ ```python! if 你的狗會倒立: 我就會倒立 else: 你的貓會倒立 ``` ---- ### 所以如果你的狗會倒立,我就會倒立 ### 你的狗不會倒立,你的貓就會倒立! #### 厲害吧! ---- ## 例子二 ```python! if 你的分數低於60分: 不及格 else: 及格 ``` --- ## 除了可以用else以外 ## 還可以用elif來重新假設!(否則如果) ---- # 假設分三個等級 ## IQ>180是天神 ## 180>=IQ>90是美女 ## 90>=IQ就是帥哥(爆發型態) ---- ### 你可以這麼說: ```python if IQ>180: 就是天神! elif IQ>90: 就是美女: elif IQ>0: 就是帥哥(爆發型態)! else: 你是外星人吧... ``` ---- ## 小練習 ## 輸入分數判斷是否及格 output ```python! 輸入你的成績:65 及格 輸入你的成績:25 你被當了 ``` ---- ```python grade = int(input(輸入你的成績:)) if grade > 60: print("及格") elif grade < 60: print("你被當了") ``` --- ## 控制流程就這樣而已! --- # 迴圈(loop) ---- ## 什麼是loop呢? ---- ## 滿足某條件後重複執行的函數 ### 而我們今天來介紹兩種用法 --- ## 第一種用法是while ---- ### example ```python name = input("輸入你的名字:") while name == "": print("你沒有輸入你的名字") name = input("輸入你的名字:") print(f"Hello {name}") ``` ---- ### 用int(整數)也是OK的喔 ```python age = int(input("輸入你的年齡:")) while age < 0 print("年齡不得為負數") age = int(input("輸入你的年齡:")) print(f"你的年齡為{age}歲") ``` --- ## 另一個用法是for ---- ## example ```python for x in range(1, 11, 2): print(x) ``` ##### 第一格 1 是指x的初值,第二格 11 是指當x小於11重複執行此段落,第三格是每執行一次x加2 ---- ## example 數字也可以倒過來 ```python for x in reversed(range(1,11,1)):#用reversed可以把數字倒過來 print(x) print("Happy new year!!") ``` ---- ## 練習一下 階層,求n!該怎麼寫呢? (限制用迴圈) ---- ## 答案 ```python sum = 1 temp = int(input()) for x in range(1, temp+1, 1): sum = sum*x print(sum) ``` ---- ## 進階練習 求1!+2!+...+n!=? ---- ## 答案 ```python sum1 = 0 temp = int(input()) for x in range(1, temp+1, 1): sum2 =1 for y in range(1, x+1, 1): sum2 = sum2*y sum1 = sum1+sum2 print(sum1) ``` --- # 函數 ~~(喔幹超麻煩的...)~~ ---- # *def* ## 你可以定義一個任何你想要的函數 ---- # 公式: ## def 名字(你要帶入的參數): ## 程式碼 ---- ### 舉個例子吧! ```python! def hotdog_is_hot(hotdog): if hotdog = hot: print("Hotdog is hot") else: print("Hotdog is cold") ``` ## 這個是比喻不能執行 ---- ## 可為甚麼執行不了呢??? ---- ## ㄟㄟ,你只是定義ㄟ,大哥~ ## 要執行阿!!!!! ---- ## 從我們剛才的例子開始好了 ```python! def hotdog_is_hot(hotdog): if hotdog == 'hot': return 'hot' else: return 'cold' cookie = input() monkey = input() cookie = hotdog_is_hot(cookie) monkey = hotdog_is_hot(monkey) ``` ## 這只是比喻,不能執行 ---- ## 所以我們假設cookie是熱的,那他就會告訴你是hot的! ## 如果是冷的,就會各告訴你是cold的! ## moneky也是一樣的! ---- ## (等等為甚麼monkey有冷熱差別...) ~~不會是...停屍間...?~~ --- # 補充 ---- return 總共有4種不同的方法 ---- 第一種:一個參數且會 return ```python def wow(a): a=a+a return a print(wow(5)) ``` ---- 第二種:2 個參數且會 return ```python def ahhh(a,b): c=a*b return c print(ahhh(5,3)) ``` ---- 第三種:0 個參數且會 return ```python def CCCC(saysomething): print(saysomething) return 0 F=CCCC("weeeee") print(F) ``` ---- 第四種:0 個參數且不會 return ```python def OMG(a,b): c=a*b return a=5 b=3 F=OMG(a,b) print(F) ``` ---- return多個值 ```python def ggg(): return 1, 1 print(ggg()) ``` --- ## 使用import引入內建函數 ---- ### 你可以利用import來引入更多的函數 ---- ### 今天就來講如何使用import math ---- ## 首先你要在程式第一句打上這個 ```python import math ``` ---- ## 這樣就可以引入函數了喔! ---- ## math 用法 ```python import math x = 13.1 y = 19.9 z = 9 print(math.pi) #圓周率pi print(math.e) #自然數e result = math.ceil(x) #無條件進入法 result2 = math.floor(y) #無條件捨去法 result3 = math.sqrt(z) #開平方根 print(result) # 14 print(result2) # 19 print(result3) # 3.0 ``` ---- ## 額外補充(以下不需import) ```python x = 10.6 y = -46 z = 0 #python好用的內建數學函數 result = round(x) #四捨五入 result2 = abs(y) #絕對值 result3 = pow(2 , 4) #2的4次方 result4 = pow(2 , 4 , 5) #2的4次方除以5的餘數 result5 = max(x, y, z) #取最大值 result5 = min(x, y, z) #取最小值 print(result) # 11 print(result2) # 46 print(result3) # 16 print(result4) # 1 print(result5) # -46 ``` --- | Column 1 | Column 2 | Column 3 | | -------- | -------- | -------- | | Text | Text | Text | | Column 1 | Column 2 | Column 3 | | -------- | -------- | -------- | | Text | Text | Text | # 補充二 ## 邏輯運算子 ---- ## 什麼是邏輯運算子? ---- ## and(和), or(或是), not(非) ### 以上就是邏輯運算子 ---- ```python a = 10 b = 10 c = 10 if a == b and a == c: print("完全相等") else: print("不完全相等") ``` output ```python 完全相等 ``` ---- ```python a = 10 b = 10 c = 15 if a == b or a == c: print("部分相等") else: print("不相等") ``` output ```python 部分相等 ``` ---- ```python a = 10 b = 10 c = 10 if not a == b and a == c: print("完全相等") else: print("不完全相等") ``` output ```python 不完全相等 ``` --- # 統整一下今天看到的運算子 ---- ```python a <= b #a小於等於b a >= b #a大於等於b a == b #a等於b,不要和a = b搞混 a == b and b == c #a等於b而且b等於c a == b or b == c #a等於b或b等於c ``` --- # 上次沒講完的bool柏林 ---- ## 他真的很簡單,他就是代表True或False ```python 變數 = True #就醬 變數 = False ``` ---- ## 至於怎麼和if搭配呢? ---- ## example ```python 美宣撞見副社與公關偷情現場 = True if 美宣撞見副社與公關偷情現場: #如果此事為真(True) print("等一下我還沒上車") else: # 如果不是就是錯的(False) print("不要瞎掰好嗎") ``` ---- ## 輸入用法 ```python 美宣撞見副社與公關偷情現場 = bool(input("美宣撞見副社與公關偷情現場是真的嗎?!:")) if 美宣撞見副社與公關偷情現場: #如果此事為真(True) print("等一下我還沒上車") else: # 如果不是就是錯的(False) print("不要瞎掰好嗎") ``` --- ## loop補充 ---- ## loop可以搭配continue和break使用 ---- # continue ---- ## continue可以將迴圈推回起點,但其中的值不會變動 ---- ## continue example ```python for x in range(1, 20, 1): if x == 10: continue else: print(x) ``` ---- ## 輸出後你會發現10不見了,因為當x等於10的時候if成立執行continue,loop就回到起點不會把x輸出 ---- # break ---- ## break可以直接中斷整個迴圈 ---- ## break example ```python for x in range(1, 21, 1) if x == 13: break else: print(x) ``` ---- ## 你會發現輸出倒12就結束了,因為x到13時迴圈就被中斷了,所以只會輸出到12
{"metaMigratedAt":"2023-06-17T12:11:20.048Z","metaMigratedFrom":"YAML","title":"條件式敘述與簡易函數","breaks":true,"description":"output","contributors":"[{\"id\":\"9d72ebb6-005c-4070-b9d6-ab4931f5153c\",\"add\":4791,\"del\":982},{\"id\":\"a471ce55-aa80-4ce1-9f35-e4cda218faf7\",\"add\":88,\"del\":268},{\"id\":\"9ac99379-42a4-44d2-a2ff-ae2b359fcf5c\",\"add\":587,\"del\":4},{\"id\":\"93514e52-4a78-40e8-a4f9-fb853aa5a5f6\",\"add\":753,\"del\":627},{\"id\":\"91eb9fab-5ee7-4263-9029-085c01058641\",\"add\":1759,\"del\":122}]"}
    431 views
   Owned this note