[英]Returns None instead of False
So I'm working on a question on CodingBat, a website that provides JS and Python practice problems.所以我在CodingBat上做题,这是一个提供JS和Python练习题的网站。 I've encountered a unexpected output. Btw here's the link to the question: https://codingbat.com/prob/p135815 .
我遇到了意外的 output。顺便说一句,这是问题的链接: https://codingbat.com/prob/p135815 。 In theory my code should return False but it returns none when I put print(squirrel_play(50, False))
理论上我的代码应该返回 False 但是当我把 print(squirrel_play(50, False))
Code:代码:
def squirrel_play(temp, is_summer):
if is_summer:
if temp <= 100:
if temp >= 60:
return True
elif temp <= 60:
return False
elif temp >= 100:
return False
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
elif temp >= 90:
return False
elif temp <= 60:
return False
when I run that with print(squirrel_play(50, False)), I get None (I should get False) Why???当我用 print(squirrel_play(50, False)) 运行它时,我没有得到(我应该得到 False)为什么???
Did you try to debug it?你试过调试它吗? With
squirrel_play(50, False)
it will fall into:使用
squirrel_play(50, False)
它将落入:
def squirrel_play(temp, is_summer):
if is_summer:
if temp <= 100:
if temp >= 60:
return True
elif temp <= 60:
return False
elif temp >= 100:
return False
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
# HERE ( 50 is less than 90 but not greater than 60 )
# and you have no return statement for this case
elif temp >= 90:
return False
elif temp <= 60:
return False
With your parameter of is_summer of False, you're in the 2nd conditional block:使用 False 的 is_summer 参数,您处于第二个条件块中:
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
elif temp >= 90:
return False
elif temp <= 60:
return False
Then follow this block:然后按照这个块:
if temp <= 90:
if temp >= 60:
return True
What is happening here is that you never get to the elif temp <= 60
because you are in the first conditional instead.这里发生的事情是你永远不会达到
elif temp <= 60
因为你处于第一个条件。 You could only ever get to the elif below if you didn't satisfy the first condition.如果你不满足第一个条件,你只能到达下面的 elif。
At the end of this if temp <= 90
block the entire conditional chain ends and your function returns the default value of None
because you didn't provide another return value.最后
if temp <= 90
块整个条件链结束,你的 function 返回默认值None
因为你没有提供另一个返回值。
You can maybe more clearly see this by making the entire code read:通过阅读整个代码,您可能会更清楚地看到这一点:
def squirrel_play(temp, is_summer):
if is_summer:
if temp <= 100:
if temp >= 60:
return True
elif temp <= 60:
return False
elif temp >= 100:
return False
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
else:
return "This is where I'm returning with 50, and True as my parameters"
elif temp >= 90:
return False
elif temp <= 60:
return False
The way that you have currently coded it, in your你目前编码的方式,在你的
if temp <= 90:
if temp >= 60:
return True
elif ....
if the first if
test evaluates True but the second one evaluates False, then no return
statement is reached (bear in mind that the subsequent elif
tests are not performed because the first if
evaluated true), and the function therefore returns None
.如果第一个
if
测试评估为 True 而第二个评估为 False,则不会到达return
语句(请记住,后续的elif
测试不会执行,因为第一个if
评估为 true),因此 function 返回None
。
In fact you can simplify the function making use of chained comparison operators :事实上,您可以使用链式比较运算符简化 function :
def squirrel_play(temp, is_summer):
if is_summer:
return 60 <= temp <= 100
else:
return 60 <= temp <= 90
If you don't return a value from a Python function, None
is returned by default.如果您不从 Python function 返回值,则默认返回
None
。 I believe that what is happening here is that because you are using elif
statements, since the clause if not is_summer: if temp <= 90:
is being entered, the final clause elif temp <= 60
is not being reached.我相信这里发生的事情是因为您正在使用
elif
语句,因为正在输入if not is_summer: if temp <= 90:
子句,因此未达到最终子句elif temp <= 60
。 Therefore the function gets passed all of the if
/ elif
statements without returning a value, and returns None
.因此 function 在没有返回值的情况下通过了所有
if
/ elif
语句,并返回None
。
A simple solution is to replace all of the elif
s with if
s.一个简单的解决方案是将所有
elif
替换为if
。 Then print(squirrel_play(50, False))
returns False
(for me at least).然后
print(squirrel_play(50, False))
返回False
(至少对我而言)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.