12. Printing out a list item by item in a function


#1

I saw another post about this one returning:
"Oops, try again. The body of your function should not contain any references to 'n'."

And the solution was something like refresh the page or try another browser.

The problem is, I already try refreshing with chrome, firefox and edge but still got the same error in all of them and still the code is printing the result:
3
5
7
3
5
7
None

My code is the following
n = [3, 5, 7]

for i in range(0, len(n)):
____print n[i]

def print_list(x):
____for i in range(0, len(x)):
________print x[i]

print_list(n)


#2

:frowning:
I already "Solve the problem" by deleting the first for with the print n[i], now I'm not sure why that was causing the error even though that part of the code was already included by the exercise and it was not included in the definition of the function...

Sorry for the trouble


#3

I had the same problem, thanks for that solution :slight_smile:

I guess when they say "use existing code as scaffold", the implication is that the scaffolding should be removed when the work is done...


#4

possible, i think is what they mean is: place this loop (the scaffold) inside the function

but both is possible


#5

n = [3, 5, 7]

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

print_list(n)

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

https://www.codecademy.com/courses/python-beginner-nzzVa/3/1?curriculum_id=4f89dab3d788890003000096#

Please help.


#6

look at this code:

n = [3, 5, 7]

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

print_list(n)
print_list([1,2])

it throws an index error for the second function call with argument [1,2], how can we make a function work for any list we supply as argument at function? Maybe we can use the function parameter (x)? argument values are passed along into the function parameter


#8

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