Cate
Cate

Reputation: 1297

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?

Upvotes: 116

Views: 137587

Answers (4)

roippi
roippi

Reputation: 25954

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 what the recursive call returns:

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

Upvotes: 146

Oghli
Oghli

Reputation: 2340

I think to understand more what actually going on in your recursive function you should try to debug your code. There is interesting visualizing code execution tool that I recommend called Python Turor.

I will try this test case on your recursive function and visualize the execution process:

First input my_var as x then enter my_var as a.

You can see from the debugging visualizer that when my_var = a it will execute the return statement.

enter image description here

Then the recursive function will return the input value a at this line of code in the recursive function.

enter image description here

After that it will go to execute get_input() function again which it doesn't return any value that's the reason the final value of print('got input:', get_input()) is None.

enter image description here

If your replace get_input() call inside recursive function with return get_input() It will return my_var value which is a in this test case.

enter image description here

Hope this demonstration using Python Tutor debugging visualization tool would be helpful in clarifying the execution process of recursive function.

Upvotes: 0

user6348168
user6348168

Reputation: 45

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())

Upvotes: 2

Simon
Simon

Reputation: 131

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.

Upvotes: 13

Related Questions