# 条件分岐(if、else、elif) ###### tags: `Python-老師` ## 条件分岐(Bunki)とは? プログラムは記述(Kijutsu)した順に実行されていきます。ただある条件によって処理を分けたい場合もあります。 「条件を満(mi)たしているかどうかで、やることを変えてね!」な命令が「条件分岐処理(ジョウケンブンキショリ)」です。 :::info *條件判斷* *程式雖會照著撰寫的順序執行,但亦會有希望程式依照條件執行的情況。* *「要依照條件式是否成立,而改變做的事情喔!」等的命令, 稱為「條件判斷處理」* ::: ## if if は指定した条件式が真の時にだけ処理を実行します。 :::info *if 只有在當指定的條件式為真時執行處理。* ::: ``` if 条件式: 条件式がTrueの時に実行する文 ```  **Example:** 輸入分數,並印出,如果分數不到60顯示"Fail grades"。 ```python= grades = int(input("Input Grades: ")) if(grades < 60): print("Fail grades") print("Grades:", grades) ``` **Results:** ``` >>>Input Grades: 70 Grades: 70 >>>Input Grades: 59 Fail grades Grades: 59 ``` ## インデント - 縮排(indentation) インデントとは、行頭に空白を入れて文字を入れて字下げを行う事を言います。 pythonにおけるインデントは非常に重要です。なぜなら、pythonは同じ数の空白でインデントされたまとまりを一つのブロックと認識する為です。 Pythonのインデントには通常、4個の空白文字を使用します。(tab key) :::info *縮排,指的是在行的開頭插入空白使文字後退* *在Python縮排是非常重要的。因為Python會將使用同數目的空白所進行縮排的程式碼,視為一個區塊。* :::  ## if else 条件式のTrueとFalseで、処理を分けたい場合、ifとelseを組み合わせて行い ます。条件式が成り立たない場合のみ、elseブロックの処理が実行されます。 :::info *若想用True和False來區分處理,則結合if和else來使用。唯有在條件式不成立時,else區塊的處理才會被執行。* ::: ``` if 条件式: 条件式がTrueの時に実行する文 else: 条件式がFalseの時に実行する文 ```  **Example2:** 判斷輸入是否為 0 ```python= number = int(input('Enter a number: ')) if number == 0: print(number,'is 0') else: print(number,'not 0') ``` **Results2:** ``` Enter a number: 10 0 is 0 Enter a number: 13 10 not 0 ``` **Example3:** 判斷輸入的成績是否及格 ```python= score = int(input('Enter a score: ')) if score >= 60: print(score,':PASS') else: print(score,':FAIL') ``` **Results3:** ``` Enter a score: 26 26 :FAIL Enter a score: 90 90 :PASS ``` ## ifのネスト if文の中にif文を入れ子にして、条件分岐を多段にできます。 :::info *if 的巢狀* *在if文之中,放入if文使之成為巢狀,可讓條件判斷成為多層判斷。* ::: ``` if 条件式1: 条件式1が真の時に実行する文 ... else: if 条件式2: 条件式1が偽で条件式2が真の時に実行する文 ... else: if 条件式3: 条件式1及び条件式2が偽で条件式3が真の時に実行する文 ... else: すべての条件式が偽のときに実行する文 ... ``` **Question:** 將輸入成績區分為 A(100 ~ 90), B(89 ~ 80), C(79 ~ 70), D(69 ~ 60) 和 F(59 ~ 0) 等 5 個等級。 **Results4:** ``` score: 90 Grade is: A score: 40 Grade is: F ``` **Example4-1:** ```python= score = int(input("score: ")) if score >= 90: print('Grade is: A') else: if score >= 80: print('Grade is: B') else: if score >= 70: print('Grade is: C') else: if score >= 60: print('Grade is: D') else: print('Grade is: F') ``` ネストにするとインデントが増えて見づらくなります。elifや論理演算を使った方がすっきりするでしょう。 :::info *如果寫成巢狀則縮排會增加,反而變得看不清楚。這時使用elif、邏輯運算子會使code比較清楚。 ::: ## elif if文の条件には引っかからなかったけど、別の条件に当てはまったらelseとは別の処理がしたい。といったときはどうするのでしょうか。これはelifで実現できます。 :::info *如果沒有滿足if文的條件,卻又想要其他條件成立並做出與else不同的處理,這時該怎麼辦呢?* ::: ``` if 条件式1: 条件式1がTrueの時に実行する文 ... elif 条件式2: 条件式1がFalseで条件式2がTrueの時に実行する文 ... elif 条件式3: 条件式1及び条件式2がFalseで条件式3がTrueの時に実行する文 ... else: すべての条件式がFalseのときに実行する文 ... ```  **Example4-2:** ```python= score = int(input("score: ")) if score >= 90: print('Grade is: A') elif score >= 80: print('Grade is: B') elif score >= 70: print('Grade is: C') elif score >= 60: print('Grade is: D') else: print('Grade is: F') ```  --- ## Exercise ### Exercise - 1 次の要求を満たすプログラムを作成してください。 1. 数字を1つ入力することが出来る 2. 入力した数字が奇数(きすう)であれば「Odd number」と出力する 3. 入力した数字が偶数(ぐうすう)であれば「Even number」と出力する :::info *請做出滿足以下要求的程式* *1.可輸入一個數字* *2.若輸入的數字為奇數則輸出「Odd number」* *3.若輸入的數字為偶數則輸出「Even number」* :::  **Code Example:** ```python= x = int(input()) if x % 2 == 0 : print("{} is an Even number.".format(x)) else : print("{} is an Odd number.".format(x)) ``` ### Exercise - 2 ユーザーに数字(西暦)を入力させ、閏年(じゅんねん)であるかを判断することが出来るプログラムを作成してください。判断基準は以下の通り: 1. 数字が4の倍数であればその年は閏年「leap year」と表示 2. 但し数字が100の倍数であれば2.よりも優先されその年は平年「Normal year」と表示 3. 更に数字が400の倍数であれば3.よりも優先されその年は閏年「leap year」と表示 :::info *請製作出一個程式,讓使用者輸入一個數字(西曆)後,判斷是否為閏年。其判斷基準為下:* *1.數字若為4的倍數則那一年為閏年並輸出「leap year」* *2.但數字若為100的倍數,則為平年並輸入「Normal year」* *3.而數字若為400的倍數則為閏年並輸出「leap year」* :::  **Code Example(if else):** ```python= year = int(input('Please input year:')) if(year % 400 == 0): print('{} is a leap year.'.format(year)) else: if(year % 100 == 0): print('{} is a normal year.'.format(year)) else: if(year % 4 == 0): print('{} is a leap year.'.format(year)) else: print('{} is a normal year.'.format(year)) ``` **Code Example(elif):** ```python= if( year % 400 == 0): print('{} is a leap year.'.format(year)) elif( year % 100 == 0): print('{} is a normal year.'.format(year)) elif( year % 4 == 0): print('{} is a leap year.'.format(year)) else: print('{} is a normal year.'.format(year)) ``` **Code Example(and or):** ```python= y = int(input()) if y % 4 ==0 and y % 100 != 0 or y % 400 == 0: print("{} is a leap year.".format(y)) else : print("{} is not a leap year.".format(y)) ``` --- ### 方 if ```python= g = int(input()) if g >= 60: print("Good") else: print("Fail grades") print("garades = {} ".format(g)) ``` ```python= s = int(input("你的成績:")) if s >=90 : print("A") elif s>=80: print("B") elif s>=70: print("C") elif s>=60: print("D") else: print("F") print("garades = {} ".format(s)) ``` ```python= s = int(input("你的年齡:")) if s >=65 : print("恭喜你!可以退休了!") elif s>=20: print("恭喜你!成年了!") else: print("恭喜你!還很年輕!") print("你的年齡: {}歲 ".format(s)) ``` ```python= s = int(input("請輸入數字:")) if s%2==0: print("偶數") else: print("奇數") ``` ### 塗 ```python= #判斷奇數偶數 A = int(input("數字:")) if A % 2 == 0: print("{}是偶數喔".format(A)) else: print("{}是奇數喔".format(A)) ``` if else if else ... ```python= score = int(input("分數: ")) if score >= 90: print("評分:優") elif score >= 80: print("評分:良") elif score >= 70: print("評分:可") elif score >= 60: print("評分:普") else : print("評分:你被當了") ``` ### 劉 ```python= print("enter your grade") g=int(input()) if g <60: print("your grade is {}".format(g)) print("Failed") else : print("your grade is {}".format(g)) print("Passed") ``` if else if else ... ```python= print("enter your grade") g=int(input()) if g <60: print("your grade is {}".format(g)) print("Failed") print("your rank is F") else : print("your grade is {}".format(g)) print("Passed") if g<70: print("your rank is D") else : if g<80: print("your rank is C") else: if g<90: print("your rank is B") else : print("your rank is A") #elif print("enter your grade") g=int(input()) if g <60: print("your grade is {}".format(g)) print("Failed") print("your rank is F") else : print("your grade is {}".format(g)) print("Passed") if g<70: print("your rank is D") elif g<80: print("your rank is C") elif g<90: print("your rank is B") else: print("your rank is A") #exercise number = int(input()) if number%2 ==1: print("the numer you entered is Odd number") else : print("the number you entered is Even number") ``` ### 邱 ```python= g = int(input()) if g<60: print("fail grades") else: print ("pass") print("grades={} ".format(g)) ``` if else if else ... ```python= score = int(input("偏差值: ")) if score >= 90: print('level: A / 東大京大合格') elif score >= 80: print('Level: B / 東大京大合格') elif score >= 70: print('Level: C / 東大京大合格') elif score >= 60: print('Level: D / 早慶') else: print('Level: F / MARCH') score = int(input("偏差值: ")) if score >= 90: print('level: A / 東大京大合格') elif score >= 80: print('Level: B / 東大京大合格') elif score >= 70: print('Level: C / 東大京大合格') elif score >= 60: print('Level: D / 早慶') else: print('Level: F / MARCH') x=int(input()) if x%2==0: print("{} is an Even number.".format(x)) else: print("{} is an Odd number.".format(x))
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up