## 邏輯判斷:if #### 2026資訊之芽Python班 ##### 曾子齊 --- ## 小小複習一下 ---- 計算BMI: ```python= # height輸入身高(m)、weight輸入體重(kg)並輸出BMI值 height = float(input()) weight = float(input()) BMI = weight/(height**2) print("ur BMI :", str(BMI)) ``` ---- 我想要他順便告訴我這個BMI代表: - 過輕:BMI < 18.5 - 正常:18.5 <= BMI < 24 - 過重:24 <= BMI < 27 - 肥胖:BMI >= 27 ---- ```python= # height輸入身高(m)、weight輸入體重(kg)並輸出BMI值 height = float(input()) weight = float(input()) BMI = weight/(height**2) print("ur BMI :", str(BMI)) # 程式怎麼知道要執行哪一條? print("過輕") print("正常") print("過重") print("肥胖") ``` --- ## if 語法 ---- 如果 (條件) 就 {結果1} ,否則 {結果2} ```python= if condition: # do something else : # do something else ``` ---- 如果 (成績低於60分) 就 {被當} 否則 {通過} ```python= if score < 60: print("Get F in this course.") else : print("Pass!!") ``` ---- 如果判斷式的內容是True,執行if下方縮排中的內容 否則執行else下方縮排中的內容 ![image](https://hackmd.io/_uploads/B100Q9ud-g.png) ---- 所以縮排到底是什麼? 一個tab or 四個空白(也有可能是八個) python使用縮排來判斷程式碼寫在if裡面還是外面 ```python= if a > 10 : print("我寫在if裡面,只有條件為True我才會執行") print("我寫在if外面,條件雨我無瓜") ``` 那判斷式呢? --- ## 運算子 ---- 利用 **比較運算子** 和 **邏輯運算子** 建構出判斷式 ---- 比較運算子 | 含意 | 符號 | | -------- | -------- | | 大於 | > | | 大於等於 | >= | | 小於 | < | | 小於等於 | <= | | 等於 | == | | 不等於 | != | ---- 邏輯運算子 |含意|符號| |-|-| |且|and| |或|or| |否|not| ---- 例子: ```python= if 條件 : ``` ```python= if a > 5 and b < 10 : # 如果a大於5且b小於10,就...... if b == a or a + b > 10 : # 如果a等於b或兩者之和大於10,就...... # 如果a不等於10,就...... if not a == 10 : if a != 10 : ``` ---- 條件句會回傳True或False(1/0) ```python= if True: # 一定會被執行 print("這個if到底有什麼用@@") if False: print("窩存在的意義是什麼......") ``` ---- 回傳? ```python= a = 7 b = a > 10 # False print(a > 10) if b: print("a is greater than 10.") ``` ---- = 和 == 的差別: - = : 賦值 - == : 比較是否相等 ```python= a = 5 if a == 5: print("a is 5!") else : print("a is not 5.") ``` ---- ![image](https://hackmd.io/_uploads/r1LSCezdZg.png) --- ## if-else 統整 ---- ![image](https://hackmd.io/_uploads/HktEMbMO-e.png) ---- 如果 (成績低於60分) 就 {被當} 否則 {通過} ```python= if score < 60: print("Get F in this course.") else : print("Pass!!") ``` 註: 可以沒有else ---- ![image](https://hackmd.io/_uploads/H1TlQ-GdZl.png) --- ## 練習一下 [670. 野豬騎士來囉](https://tioj.sprout.tw/contests/40/problems/670) --- ## 巢狀if ---- if裡面可以放if嗎? ```python= if a > 10 : # do something # 例如我想要再判斷a有沒有大於20 ``` ---- 當然可以! ```python= if a > 10 : # do something if a > 20: pass # 可以輸入pass讓這部分程式碼先空著 ``` ---- 我們希望巢狀if不要太多...... ```python= if a > 10: if b > 20: if is_vip: if current_time < 8: ...... ``` 改成這樣! ```python= if a > 10 and b > 20 and is_vip and current_time < 8: # do something ``` --- ## if-elif-else ---- 如果想要依序判斷,會變得很麻煩: ```python= if BMI >= 27: print("肥胖") else: if BMI >=24 : print("過重") else: if BMI >= 18.5: ...... ``` 這時就需要elif! ---- 如果......否則如果......否則...... ```python= if score > 90: print("Average") elif score > 80: print("Bad") elif score > 70: print("Cannot eat dinner") elif score > 60: print("Do not back home") else : print("Failure") ``` 如你所見,elif可以加無限多個。 ---- ![image](https://hackmd.io/_uploads/H1I5PZG_Zg.png) --- 練習! [671. 三元排序](https://tioj.sprout.tw/contests/40/problems/671) 嘗試自己寫出BMI判斷程式 --- ## 其他補充 ---- 使用小括號安排邏輯判斷的順序: ```python= if a > 10 and a < 20 or 100 < b < 200: pass ``` ```python= if ((a > 10) and (a < 20)) or ((100 < b) and (b < 200)): pass ``` ---- 判斷式太長可以使用\排版 ```python= # 注意\後不可有空白 if ((a > 10) and (a < 20)) or \ ((100 < b) and (b < 200)): pass ``` ---- 特別的運算子is ```python= if a is b: pass print(id(a), id(b)) ``` 和==很像但不完全一樣,is比較的是記憶體位置 記憶體位置可以使用id()函式得知 [兩者比較連結](https://clay-atlas.com/blog/2020/08/04/python-cn-equal-is-difference/) ---- 偷懶的寫法:三元運算子 ```python= if condition: ans = "Yes" else : ans = "No" ``` 一條寫完 ```python= ans = "Yes" if condition else "No" ``` ---- 條件判斷會將 None、[]、{}、""、0 判斷為False ```python= a = None if a: print("因為False所以不會被執行") ``` ---- ## 回家作業 [796. 外送迷陣](https://tioj.sprout.tw/contests/40/problems/796) [759. 超級貓貓星際漫遊-1](https://tioj.sprout.tw/contests/40/problems/759) [760. 超級貓貓星際漫遊-2](https://tioj.sprout.tw/contests/40/problems/760)
{"title":"2026資訊之芽Python班:邏輯判斷","description":"邏輯判斷、if","contributors":"[{\"id\":\"9b0a9794-5c7e-4a79-baad-660057d9ee00\",\"add\":4647,\"del\":331,\"latestUpdatedAt\":1772333216177}]"}
    80 views