再客製化:elif
你也可以在臥室四處看看。範例程式碼包含一個 elif 區塊,會檢查 room 是否等於 "bed"。若是,就會印出 "looking around in the bedroom."。
接下來換你了!在第二個控制結構加上類似的內容,針對不同的 area 值進一步客製化訊息。
本練習屬於課程
Python 中級
練習說明
在第二個控制結構加入一個 elif,當 area 大於 10 時,印出 "medium size, nice!"。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define variables
room = "bed"
area = 14.0
# if-elif-else construct for room
if room == "kit" :
print("looking around in the kitchen.")
elif room == "bed":
print("looking around in the bedroom.")
else :
print("looking around elsewhere.")
# if-elif-else construct for area
if area > 15 :
print("big place!")
else :
print("pretty small.")