開始使用免費開始

加入 else

在這支指令碼中,roomif 結構已加入 else 敘述,當條件 room == "kit" 的結果為 False 時,會印出 "looking around elsewhere."

你能用相同的做法,替 areaif 結構再增加一些功能嗎?

本練習屬於課程

Python 中級

檢視課程

練習說明

在第二個控制結構中加入一個 else 敘述,讓當 area > 15 的結果為 False 時,會印出 "pretty small."

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define variables
room = "kit"
area = 14.0

# if-else construct for room
if room == "kit" :
    print("looking around in the kitchen.")
else :
    print("looking around elsewhere.")

# if-else construct for area
if area > 15 :
    print("big place!")
編輯並執行程式碼