if - elif 語句

引入:如果平時執行的過程超過兩個分支,則使用if-elif語句

if-elif 語句結構

if 判斷條件:

要執行的代碼

elif 判斷條件:

要執行的代碼

……

else:

要執行的代碼

 

判斷條件:一般爲關係表達式或者bool類型的值

執行過程:程序運行到if處,首先判斷if所帶的條件,如果條件成立,就返回True,則執行要執行的代碼;

如果條件不成立,依次往下判斷elif的條件,如果又滿足則執行相應的代碼,如果條件都不成立,則執行else下要執行的代碼。

 

示例1:輸入小王(語文,英語,數學)成績(單科滿分100分)判斷成績評定等級

學員評定標準如下:

·         成績>=90分:A

·         90>成績>=80分:B

·         80>成績>=70分:C

·         70>成績>=60分:D

·         成績<60分:E

 

chinese_result = int(input("請輸入語文成績:"))

maths_result = int(input("請輸入數學成績:"))

englist_result = int(input("請輸入英語成績:"))

avg_result = (chinese_result + maths_result + englist_result) / 3

if avg_result >= 90:

    print("你的平均分爲:%.2f,成績的總和評定爲:A" % avg_result)

elif avg_result >= 80 and avg_result < 90:

    print("你的平均分爲:%.2f,成績的總和評定爲:B" % avg_result)

elif avg_result >= 70 and avg_result < 80:

    print("你的平均分爲:%.2f,成績的總和評定爲:C" % avg_result)

elif avg_result >= 60 and avg_result < 70:

    print("你的平均分爲:%.2f,成績的總和評定爲:D" % avg_result)

else:

    print("你的平均分爲:%.2f,成績的總和評定爲:E" % avg_result)

 

結果:

C:\python\python.exe C:/python/demo/file2.py

請輸入語文成績:45

請輸入數學成績:34

請輸入英語成績:56

你的平均分爲:45.00,成績的總和評定爲:E

 

Process finished with exit code 0

 

示例2:(之前小紅花案例第二次優化)

在控制檯應用程序中輸入小王(語文,英語,數學)成績(單科滿分100分)

判斷:

1)如果有一門是100

2)如果有兩門大於90

3)如果三門大於80

滿足以上一種情況,則獎勵一朵小紅花

 

chinese = int(input("請輸入語文成績:"))

maths = int(input("請輸入數學成績:"))

englist = int(input("請輸入英語成績:"))

get_course = ""

if chinese == 100 or maths == 100 or englist == 100:

    if(chinese == 100): get_course += "語文、"

    if(maths == 100): get_course += "數學、"

    if(englist == 100): get_course += "英語、"

    print("你的%s得了100分,獎勵一朵小紅花" % get_course)

elif(chinese >= 90 and maths >= 90) or (chinese >= 90 and englist >= 90) or (maths >= 90 and englist >= 90):

    if(chinese >= 90): get_course += "語文、"

    if(maths >= 90): get_course += "數學、"

    if(englist >= 90): get_course += "英語、"

    print("你的%s大於90分,獎勵一朵小紅花" % get_course)

elif chinese >= 80 and maths >= 80 and englist >= 80:

    print("你的三個科目語文、數學、英語都大於80分,獎勵一朵小紅花")

else:

    print("沒有獲得小紅花,下次努力哦!")

 

結果:

C:\python\python.exe C:/python/demo/file2.py

請輸入語文成績:87

請輸入數學成績:86

請輸入英語成績:91

你的三個科目語文、數學、英語都大於80分,獎勵一朵小紅花

 

Process finished with exit code 0

 

提問:有了 if- if- , if-else, 爲何還需要 if-elif-elif-else ?

1.png

左邊的條件選擇是4個部分,後面兩個語句是一個整體,三個if語句加一個if-else,else是和它最近的if匹配的。

右邊的是一個整體,執行的過程過程中只能相應一個語句。

 

示例3輸入一個月份,判斷該月份是屬於哪個季節:

冬季(12-2月)春季(3-5月) 夏季(6-8月)秋季(9-11月)

month = int(input("請輸入一個月份:"))

if(month == 12 or month == 1 or month == 2):

    print("%d月是冬季" % month)

elif(month == 3 or month == 4 or month == 5):

    print("%d月是春季" % month)

elif(month == 6 or month == 7 or month == 8):

    print("%d月是夏季" % month)

elif(month == 9 or month == 10 or month == 11):

    print("%d月是秋季" % month)

 

結果:

C:\python\python.exe C:/python/demo/file2.py

請輸入一個月份:8

8月是夏季

 

Process finished with exit code 0



這是給大家更新的最後一篇Python文檔了,最近一段時間我要去進修一下自己,準備重新換個工作,等學完了在和大家一起交流,謝謝大家的關注!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章