Printing out a list item in a function


#1




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


I am not referencing the variable list 'n' inside my function so I dont know.


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

Hi, @joe_head123 ,

The message from Codecademy may be misleading. This original loop needs to be removed; it was just given to the user as a model ...

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

#3

Hint
You can simply place the code on lines 3 - 4 inside a function definition. Make sure to indent properly! And you'll have to change the n to x

it tells me to use it.


#4

n = [3, 5, 7]

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

print_list(n)

Error:

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


#5

In the code in your original post, you used the loop that you were given correctly inside the function, so the function was sound and ready to use. You just needed to get rid of the original loop for your code to pass.


#6

n = [3, 5, 7]

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

This worked lol I get it now it wanted me to remover the first code that was originally placed there.
Thanks for the help ! Love this site!


#7