Python選擇結構語句分為if語句、if else 語句 、if elif else語句
1.if語句
if語句用於檢測表達式是否成立,如果成立則執行if語句內的語句塊
if(表達式):
語句塊
>>>c = 10
>>>if c > 5
print('c > 5')
運行結果: c > 5
2.if else語句
if(表達式):
語句塊1
else:
語句塊2
>>>name = 'Damon'
>>>if(name == 'Tom'):
print("hello ,Tom")
else :
print ("hello" , +name)
運行結果:hello,Damon
3.if elif else
if(表達式1):
語句塊1
elif(表達式2):
語句塊2
else:
語句塊3
編程舉例:
# -*- coding:utf-8 -*- score = input("請輸入學生成績:") score = (int)score if(score >= 90: print('A') elif (score >= 80): print('B') elif (score >= 60): print('C') else: print('D')