1. 程式人生 > 實用技巧 >Python_分支語句

Python_分支語句

# if not None:
# print(None)
#分支語句又叫條件判斷語句
a = list (range(100,900))#輸出一個區間列表
b = range(100,900)
print(b)
#輸入一個分數,如果大於等於60分,列印及格,否則不及格
# score = int(input("請輸入分數:"))#input方法返回值為str
# print(score)
# if score >=60 :
# print("及格")
# else:
# print("不及格")
#輸入一個分數,如果大於等於90分,顯示優秀,如果大於等於80分,顯示不錯,如果大於等於60分,顯示及格,否則不及格
# score = int(input("請輸入分數:"))
# if score >= 90:
# print("優秀")
# elif score >=80:
# print("不錯")
# elif score >=60:
# print("及格")
# else:
# print("不及格")
#if--elif--elif--else是一組的,if--if--if每個if是獨立的分支,相互之間是沒有影響的
"""
比如:早上上班是坐公交還是坐地鐵,中午是吃黃燜雞米飯還是豬腳飯,晚上是6點下班還是7點下班
"""
#elif,else,可以省略,需要才寫,不需要可以不寫
if " ": #True,非0," "空格字元為真,False,None,""空字串為假,
pass#佔位符,當沒有語句要寫時,為了避免語法錯誤,就放一個pass在程式裡面
print("真")

#if巢狀
#如果一個人的年齡大於60歲,且為男性,就列印“老先生”
age = int(input("請輸入您的年齡:"))
gender = input("請輸入您的性別male or female:")
# if age>60:
# if gender=="male":
# print("老先生")
# else:
# print("老女人")
# else:
# print("年輕人")
import time
start_time=time.time()
if age>60 and gender=="male":
print("老先生")
else:
print("年輕人")
end_time= time.time()
print(end_time-start_time)#統計執行時間