用Python返回价值作业

时间:2019-11-07 01:33:53

标签: 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分谢谢

3 个答案:

答案 0 :(得分:0)

您有重叠的条件。如果温度高于-10,则始终报告“扭矩”。您也有拼写错误,例如“温度”。

def wear_the_right_thing(temperature):
if temperature < -10:
    return "parka and toque"  
elif temperature >= -10 and temperature <= -5:
    return "toque"
elif temperature > -10 and temperature <= 0:
    return "sweater"
elif temperature > 0 and temperature <= 20:
    return "t-shrit"
elif temperature > 20:
    return "shorts" 

答案 1 :(得分:0)

您需要使用and在一个if语句中给出多个条件。 例 if temperature > -10 and temperature < 0 : return "toque"

答案 2 :(得分:0)

您的情况有很多问题,请尝试类似的操作

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"