Why does my recursive function return None?

Posted on

Solving problem is about exposing yourself to as many situations as possible like Why does my recursive function return None? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Why does my recursive function return None?, which can be followed any time. Take easy to follow this discuss.

Why does my recursive function return None?

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:

Type "a" or "b": a
got input: a

But, if I type something else and then “a” or “b”, I get this:

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?

Asked By: Cate

||

Answer #1:

It is returning None because when you recursively call it:

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:

>>> def f(x):
...     pass
>>> print(f(20))
None

So, instead of just calling get_input() in your if statement, you need to return it:

if my_var != "a" and my_var != "b":
    print('You didn't type "a" or "b". Try again.')
    return get_input()
Answered By: roippi

Answer #2:

To return a value other than None, you need to use a return statement.

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.

Answered By: Simon

Answer #3:

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())
Answered By: user6348168

Answer #4:

i think this code more clearly

def get_input():
    my_var = str(input('Enter "a" or "b": '))
    if my_var == "a" or my_var == "b":
        print('got input:', my_var)
        return my_var
    else:
        print('You didn't type "a" or "b". Try again.')
        return get_input()
get_input()
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *