The problem is here:
y.append(1)
The append()
method returns None
, so you can't pass its result for building the output list (you'd have to first append
to the list and then pass it, as shown in other answers). Try this instead:
def recurse(y, n):
if len(y) == n:
return y
else:
return recurse(y + [1], n)
The above solution is more in line with a functional programming style. Using append
adds an element to an existing list - which will mutate a function parameter, in general not a very good idea. On the other hand y + [1]
creates a new list each time, leaving the parameter untouched. Proponents of functional programming will tell you that's a Good Thing.