14
def foo(a):
    a.append(1)
    if len(a) > 10:
        print a
        return a
    else:
        foo(a)

Why this recursive function returns None (see transcript below)? I can't quite understand what I am doing wrong.

In [263]: x = []

In [264]: y = foo(x)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

In [265]: print y
None
0

1 Answer 1

20

You don't return anything in the else clause:

    else:
        return foo(a)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.