首页 > 解决方案 > 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 非常感谢你

标签: python

解决方案


你的条件有很多问题,试试这样的

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"

推荐阅读