Python 为什么我的递归函数返回 None?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17778372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does my recursive function return None?
提问by Cate
I have this function that calls itself:
我有这个调用自己的函数:
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
else:
return my_var
print('got input:', get_input())
Now, if I input just "a" or "b", everything works fine:
现在,如果我只输入“a”或“b”,一切正常:
Type "a" or "b": a
got input: a
But, if I type something else and then "a" or "b", I get this:
但是,如果我输入其他内容然后输入“a”或“b”,我会得到:
Type "a" or "b": purple
You didn't type "a" or "b". Try again.
Type "a" or "b": a
got input: None
I don't know why get_input()
is returning None
since it should only return my_var
. Where is this None
coming from and how do I fix my function?
我不知道为什么get_input()
要返回,None
因为它应该只返回my_var
。这是None
从哪里来的,我该如何修复我的功能?
采纳答案by roippi
It is returning None
because when you recursively call it:
它正在返回,None
因为当您递归调用它时:
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
..you don't return the value.
..你不返回值。
So while the recursion does happen, the return value gets discarded, and then you fall off the end of the function. Falling off the end of the function means that python implicitly returns None
, just like this:
因此,虽然递归确实发生了,但返回值会被丢弃,然后您就会脱离函数的末尾。从函数的末尾脱落意味着python隐式返回None
,就像这样:
>>> def f(x):
... pass
>>> print(f(20))
None
So, instead of just callingget_input()
in your if
statement, you need to return
it:
因此,您不仅需要调用get_input()
您的if
语句,还需要return
:
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
return get_input()
回答by Simon
To return a value other than None, you need to use a return statement.
要返回 None 以外的值,您需要使用 return 语句。
In your case, the if block only executes a return when executing one branch. Either move the return outside of the if/else block, or have returns in both options.
在您的情况下, if 块仅在执行一个分支时执行返回。要么将返回移到 if/else 块之外,要么在两个选项中都有返回。
回答by user6348168
def get_input():
my_var = input('Enter "a" or "b": ')
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
return get_input()
else:
return my_var
print('got input:', get_input())
回答by asylturatbek dooranov
I think that you should use while loops.
我认为你应该使用while 循环。
if my_var != "a" and my_var != "b":
print('You didn\'t type "a" or "b". Try again.')
get_input()
Consider that you type something different than "a" and "b", of course, it will call get_input
but then it is skipping the next part. Which is:
考虑到您键入的内容与“a”和“b”不同,当然,它会调用get_input
但随后会跳过下一部分。这是:
else:
return my_var
And will go directly into:
并将直接进入:
print('got input:', get_input())
So, if you use while loop as:
所以,如果你使用 while 循环:
while my_var!="a" and my_var!="b":
print('You didn\'t type "a" or "b". Try again.')
return get_input()
This way I think that you can handle it.
这样我认为你可以处理它。