[solved]print out a list item by item in a function


#1

I'm stuck on this problem as well.
here is my Code:

n = [3, 5, 7]

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

And getting this error message:

Oops, try again. Your function caused the following error: list index out of range

I don't get what I'm doing wrong... the index can't possibly be out of range.


Printing out a list item by item in a function
#2

look at this code:

# function declaration
def example(x):
    print x

# function call
example([1,2,3])

see how i pass a list as argument when calling the function? the argument is then copied into the function parameter (x)

so when you use n in your function, your function only works for the list n, not for any list you pass as argument when calling the function (x)


#3

Oh, I see. I forgot to change len(n) to len(x). Didn't see that little detail. Thanks for the reply.

EDIT: Still stuck with a different error response..

Oops, try again. The body of your function should not contain any references to 'n'

But there's no occurrences of n in the function?


#4

no, but there is still n in the loop outside the function.

The loop was a scaffold for you to place inside the function and modify. You wrote a new one, that is fine, but then you have to remove the old one


#5

Ah... didn't know that. Works now. Thanks!


#6