开始使用免费开始使用

进一步自定义: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.")
编辑并运行代码