Python 學習筆記7 條件語句 If

Python中條件語句if 是經過一條或者多條的執行語句的結果,來判斷是否執行其包含的代碼塊。python

一般會配合else、elif一塊兒使用,達到根據條件進行多個代碼塊的執行操做。spa

簡單的if code

score = 90

if score >= 95:
    print("優秀")

#沒有輸出

if 95 > score >= 80:
    print("")

#輸出: 良

 

和else 配合使用:blog

score = 90

if score >= 60:
    print("合格")
else:
    print("不合格")

#輸出: 合格

 

使用if -elif-else判斷結構:it

score = 80

if score < 60:
    print("不合格")
elif 80 > score >= 60:
    print("")
else:
    print("")

#輸出: 良

 

 在if語句中嵌套條件語句:class

score = 80

if score >= 60:
    print("經過")
    if 80 > score >= 60:
        print("")
    if 90 > score >= 80:
        print("")
    if 100 >= score >= 90:
        print("")
else:
    print("不合格")

#輸出:經過
#

 

score = 80

if score < 60:
    print("不合格")
elif score < 70:
    print("合格")
elif score < 80:
    print("")
elif score < 90:
    print("")
else:
    print("")

#輸出:良

 

目前python3.0中沒有 switch case結構,因此咱們須要靈活的使用if-elif-else結構來進行條件判斷。di

相關文章
相關標籤/搜索