我是python的新手。我想了解为什么我的else语句不起作用? else语句应返回“零”,但返回“无”。
def number_group(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
else:
"Zero"
print(number_group(10)) #Should be Positive
print(number_group(-5)) #Should be Negative
print(number_group(0)) #Should be Zero
答案 0 :(得分:1)
别忘了return语句:
def number_group(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
else:
return "Zero"
print(number_group(10)) #Should be Positive
print(number_group(-5)) #Should be Negative
print(number_group(0)) #Should be Zero
答案 1 :(得分:0)
您忘了在最后一行添加退货
# you wrote "zero"
# you should use
else:
return "Zero"