搬磚的也能學Python----if,搬磚python----if

來源:互聯網
上載者:User

搬磚的也能學Python----if,搬磚python----if

引入:如果平時執行的過程超過兩個分支,則使用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 ?

 

左邊的條件選擇是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

 

本人原創文章,非商業用途可隨意轉載,轉載請保留原文出處

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.