Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this function cause a Maximum recusion error?

I have a very simple function in python that I made while using codecademy.com. The code passed the exercise but it does cause a maximum recursion error and I do not understand why. This is what I have:

n = [3, 5, 7]

def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return double_list(x)

print double_list(n)
like image 371
MCP_infiltrator Avatar asked Feb 16 '23 15:02

MCP_infiltrator


1 Answers

Because in your double_list you are calling double_list again?

return double_list(x)

That will cause you to enter the endless loop unless you set a condition in which it should break upon.

OP already solved it, and this is his solution:

n = [3, 5, 7]

def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

print double_list(n)
like image 175
Torxed Avatar answered Feb 19 '23 10:02

Torxed