Printing out a list item by item in a function


#1

Error: Oops, try again. Your function caused the following error: maximum recursion depth exceeded

I am confused

Here is the code:

n = [3, 5, 7]

for i in range(0, len(n)):
    def print_list(x):
        return print_list(x)
    print n[i]

#2

you are calling a function within a function(recursion) but there is no base or termination case so there is infinite computation till the system runs out of resources. That's what the error is telling you


#3

I changed the code by removing the function in the function. I set the function to return x' instead. And I am getting this error:
"Oops, try again. The body of your function should not contain any references to 'n'"

Where am I getting it wrong and how do I resolve it?

Code:
n = [3, 5, 7]

for i in range(0, len(n)):
def print_list(x):
return x
print n[i]


#4

define a function before for i in range so that the following

def print_list(x):

should go first.


#5

n = [3, 5, 7]

def print_list(x):
for i in range(0, len(x)):
print x[i]

print_list(n)


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.