Why does this function cause a maximum return error?

I have a very simple function in python that I did when using codecademy.com. The code passed the exercise, but it causes the maximum recursion error, and I donโ€™t 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) 
+4
source share
2 answers

Because in your double_list you call double_list again?

 return double_list(x) 

This will cause you to enter an infinite loop if you do not establish a condition in which it should break.

The OP has already solved it, and this is its 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) 
+6
source

You forgot the base case. Each recursive function must have a base register. He continues to call himself and, therefore, reaches maximum depth.

Line

 return double_list(x) 

calls the same function again and again.

+2
source

Source: https://habr.com/ru/post/1479135/


All Articles