changed 2 years ago
Published Linked with GitHub

Python if

資訊之芽 梁安哲 2022/03/13


課程大綱 Outline


  1. 課程回顧
  2. 比較運算子
  3. 邏輯運算子
  4. 基本語法
  5. 縮排
  6. 花俏語法

課程回顧 Recap


輸出

print("萊納,你坐啊")

變數

someInteger = 10 someString = "A for apple, B for ball." someBool = False someFloat = 8.7

輸入

colossalTitan = input("城牆裡有幾隻超大型巨人?") print("早上好馬萊,現在我有" + colossalTitan + "隻超大型巨人")

Questions?


比較運算子 Comparison Operators


之前的運算子

print(1 + 3 * 3) # ? print("1" + "3" * 3) # ? print(10 / 3) # ? print(10 // 3) # ? print(2 ** 5) # ?

print(1 + 3 * 3) # 10 print("1" + "3" * 3) # 1333 print(10 / 3) # 3.3333... print(10 // 3) # 3 print(2 ** 5) # 32

如果我們想要做這樣的事情

艾蓮·葉卡:

梟他,將這個稱作"尤彌爾的詛咒"

十三年是始祖尤彌爾從力量覺醒到死去之間的時長

沒人擁有超越始祖尤彌爾的力量

只要接近這個時辰軀體就會衰弱作為容器的使命就算完成了。


翻譯成白話


  • 始祖尤彌爾的力量大於所有人的力量
  • 繼承巨人之力的人不會活超過13年

要怎麼讓電腦聽懂呢

powerYmir = 9999 powerEren = 100 statement1 = powerYmir > powerYmir # True yearYmir = 13 yearEren = 13 statement2 = yearEren <= yearYmir # True

比較運算子 continued

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

非常重要的重點

a, b, c, d = 1, 2, 3, 4 firstBool, SecondBool = a = b, c == d print(firstBool) # ? print(SecondBool) # ?

= != == (重要)

a, b, c, d = 1, 2, 3, 4 firstBool = a = b SecondBool = c == d print(firstBool) # 2 print(SecondBool) # False

Questions?


邏輯運算子 Logical Operators


如果我們還想要做這樣的事情

阿爾敏·亞魯雷特:

再次分成兩組搜索牆面,尤其是大門上方部位

無倫艾蓮何時出現賭塞牆壁都能做出對應的位置

任何時候都能看清並把握狀況的位置

並且在時機到來前都能安全藏身的位置


一樣翻譯成白話


同時滿足三個條件的地方

  • 隨時對應
  • 把握狀況
  • 安全藏身

要怎麼讓電腦聽懂呢

respondAnytime = True niceView = True safe = True optimalLocation = respondAnytime and niceView \ and safe # True

邏輯運算子 continued

Operator Name Example
and Returns True if both statements are true x < 5 and x < 10
or Returns True if one of the statements is true x < 5 or x < 4
not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

善用括號

print(not True and False or True or False) # ? print(not((True and False) or (True or False))) # False

Questions?


If


進擊的巨人again

米卡莎·阿卡曼:
如果你加入憲兵團,那麼我也會去
而要是你加入駐紮兵團,我也會照做
艾蓮,如果你不在我的身邊,一定會早死的。


再次翻譯成白話


艾蓮加入調查兵團->米卡莎加入調查兵團
艾蓮加入憲兵團->米卡莎加入憲兵團
艾蓮加入紮駐兵團->米卡莎加入紮駐兵團
(注意一個人一次只能加入一個兵團)


一樣我們想要讓電腦聽懂

erenJoinSurveyCorps = True erenJoinGarrison = False erenJoinMilitaryPoliceBrigade = False if erenJoinSurveyCorps: print("米卡莎加入調查兵團") if erenJoinMilitaryPoliceBrigade: print("米卡莎加入憲兵團") if erenJoinGarrison: print("米卡莎加入紮駐兵團")

那些空白是啥酷東西?


不合理的敘述

erenJoinSurveyCorps = True erenJoinGarrison = True erenJoinMilitaryPoliceBrigade = True if erenJoinSurveyCorps: print("米卡莎加入調查兵團") if erenJoinMilitaryPoliceBrigade: print("米卡莎加入憲兵團") if erenJoinGarrison: print("米卡莎加入紮駐兵團")

改成合理的敘述

erenJoinSurveyCorps = True erenJoinGarrison = True erenJoinMilitaryPoliceBrigade = True if erenJoinSurveyCorps: print("米卡莎加入調查兵團") elif erenJoinMilitaryPoliceBrigade: print("米卡莎加入憲兵團") else: print("米卡莎加入紮駐兵團")

if elif else 三兄弟

income = int(input()) tax = 0 if income < 0: tax = 0 elif income < 100: tax = income * 0.1 elif income < 200: tax = income * 0.2 elif income < 300: tax = income * 0.3 else: tax = income * 0.4 print(tax)

Questions?


縮排 Code Indentation (重要)


computer = 10 user = int(input()) if user > 0: if user > computer: print("you win") else: print("you lose")

computer = 10 user = int(input()) if user > 0: if user > computer: print("you win") else: print("you lose")

有哪裡不同?


為什麼?


縮排是啥?

  1. 縮排=程式碼開頭的空白。
  2. Python使用縮排表示一個區塊的程式。

錯誤範例

a, b = 3, 5 if a > b: print("a is greater than b") print("python ended")

正確範例

a, b = 3, 5 if a > b: print("a is greater than b") print("python ended")

縮排是啥? continued

  1. 空白的個數為自訂,原則上為四個空白。
  2. 同一個區塊內的空白數量需一致。
  3. 區塊內不可空白(可用pass規避)

錯誤範例 again

if 5 > 2: print("Five is greater than two!") print("Five is greater than two!")
if 5 > 2: #Five is greater than two!

正確範例 again

if 5 > 2: print("Five is greater than two!") print("Five is greater than two!")
if 5 > 2: #Five is greater than two! pass

現在是練習時間

neoj 3010


Nested if

a, b, c = 1, 2, 3 if c > b: if c > a: print("c is the biggest number")

代表我們可以這樣?


請不要


要善用邏輯運算子

a, b, c = 1, 2, 3 if c > b and c > a: print("c is the biggest number")

使用條件句的邏輯整理 (重要)

  1. 開頭一定會有if,其他都是非必要的。
  2. 上一個條件不符合時,繼續判斷下一個條件,使用elif
  3. 所有條件都不符合時做某些事,使用else
  4. if及else只會出現一次,但是可以有複數個elif
  5. if一定會出現在第一個,else一定會出現在最後一個(但是不一定有else)

Questions?


現在是練習時間

neoj 3011


花俏語法 Advanced


冗長的程式碼

condition = False a = 0 if condition: a = 1 else: a = -1

三元運算子 Ternary Operator

condition = False a = 1 if condition else -1

現在是作業時間

neoj 3401
neoj 3402


Thanks

Select a repo