Python中的返回值作业



编写一个函数,根据参数温度返回您应该穿的衣服类型。如果温度低于-10,你将穿派克大衣和戴帽(返回"派克大衣和帽子"(。如果温度在-10到0之间,请戴上帽子(返回"toque"(。如果温度大于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,非常感谢

您有重叠条件。如果温度高于-10,则总是报告"toque"。你也有拼写错误,例如"temperature"。

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" 

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

您的条件有很多问题,请尝试以下

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"

最新更新