开始使用免费开始使用

添加 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!")
编辑并运行代码