Printing out a list item by item in a function


#1

error = > The body of your function should not contain any references to 'n'

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 print_list(n)

hum.... whats wrong?


#2

Looks like your final line is indented wrong, causing n to be referenced inside it.


#3

sorry bad edit post. With right indention i ve same error.


#4

did you get this to work because its not working for me either ugh!!


#5

i ve try on text editor, it works.
And on the next exercices, with a similar way, there is no error ... perhaps a bug from the website.


#6

n = [3, 5, 7]
def print_list(x):
for i in range(0, len(x)):
x[i] + 1
return x
print (print_list(n))

I can not get this to work...can anyone help me please?


#7

edit you code please with </>


#8

This should do it:

n = [3, 5, 7]

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

#9

I had the same error, so I just removed the reference to 'n' like this:

x = [3, 5, 7]

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

#10

Adding a return after the for solved it for me


#11

why do u delete this?

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

#13

once i deleted this:

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

it took out the issue of references to n, which allowed me to proceed. I am a noobie, but it seems weird they would make us deleted the framework like that in order for it to work.


#14

Hi tonyhak can i know why u r putting for loop inside the function?and why u r changing the for loop with for i in range(0, len(x)):instead of this for i in range(0, len(n)):then print x[i] instead of print n[i]


#15

Hi to all,
I have solved it just by changing the name of the list. In fact, the problem was caused by what they wrote. I have changed everything to l and it worked :smile:

l = [3, 5, 7]
def print_list(x):
for key in x:
return key
for i in range(0, len(l)):
print l[i]


#16

They just wanted you to use it as a reference I believe. That's what they meant by using it as a scaffold.


#17

You're supposed to be using that inside of the function and changing the arguments inside of it rather than writing it again.


#18

delete the "for" that you're not using in this exercise, try it like this, it should work :wink:

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

print_list(n)`

#19

Hi,

So the reason n[i] is replaced by x[i] is because the function print_list(x) takes x as a parameter and not n.
so everything inside the function needs to call x, and not n.