python - Python中的返回值作业
问题描述
编写一个函数,根据温度参数返回你应该穿的衣服类型。如果温度低于 -10,您将穿上派克大衣和无边帽(返回“大衣和无边帽”)。如果温度在 -10 和 0 之间,请佩戴扭矩(返回“扭矩”)。如果温度大于 0 但小于 10,则穿毛衣(返回“毛衣”)。如果温度在 10 到 20 之间,请穿一件 T 恤(返回“T 恤”)。如果温度高于 20,穿短裤(返回“短裤”)。
例如:
wear_the_right_thing(25) == "shorts" wear_the_right_thing(-25) == "parka and toque" wear_the_right_thing(-5) == "toque"
这是我的代码:
def wear_the_right_thing(temperature):
if temperature < -10:
return "parka and toque"
if temperature >= -10:
return "toque"
if temperature > -10 and temerature <= 0:
return "sweater"
if temperature > 10 and temperature <= 20:
return "t-shrit"
if temperature > 20:
return "shorts"
这是我的结果(不是我标记的输出):
Result Actual Value Expected Value Notes
Fail 'toque' 'shorts' wear_the_right_thing(25)
Fail 'toque' 't-shirt' wear_the_right_thing(20)
Fail 'toque' 't-shirt' wear_the_right_thing(15)
Fail 'toque' 't-shirt' wear_the_right_thing(10)
Fail 'toque' 'sweater' wear_the_right_thing(9)
Fail 'toque' 'sweater' wear_the_right_thing(1)
Pass 'toque' 'toque' wear_the_right_thing(0)
Pass 'toque' 'toque' wear_the_right_thing(-10)
Pass 'parka and toque' 'parka and toque' wear_the_right_thing(-11)
Pass 'parka and toque' 'parka and toque' wear_the_right_thing(-30)
You passed: 40.0% of the tests
所以我没有通过测试所以可以帮助我获得 100 非常感谢你
解决方案
你的条件有很多问题,试试这样的
def wear_the_right_thing(temperature):
if temperature < -10:
return "parka and toque"
elif -10 <= temperature <= -5:
return "toque"
elif -5 < temperature <= 0:
return "sweater"
elif 0 < temperature <= 20:
return "t-shrit"
elif temperature > 20:
return "shorts"
推荐阅读
- powerapps - PowerApp 应用程序的画布布局中没有项目显示
- sql - 在 google cloud firestore 中执行 JOIN 查询
- sql - 如何解决存储过程中的性能问题?
- android - 从 android studio 的库中排除 jar 文件
- aem - 有人可以帮我实现 XSSFilter
- bash - 排除查找中的目录数组/列表
- javascript - 仅允许文本框中仅包含特定字母的数字
- python - 为什么熊猫数据框消耗的 RAM 比原始文本文件的大小要多得多?
- spark-streaming - Spark Streaming 中是否允许使用 Sleep 语句
- python - 如何根据列值在现有数据框中添加新行?