# 12/18 Printing out a list item by item in a function

Define a function called print_list that has one argument called x.
Inside that function, print out each element one by one. Use the existing code as a scaffold.
Then call your function with the argument n.

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

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

3
5
7
Traceback (most recent call last):
File “python”, line 7, in
NameError: name ‘x’ is not defined

Hi @amara_leggett ,

Because your code is not formatted, it is difficult for other users to read and debug it. After code has been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. This will enable us to see important details, such as the indentation and underscores.

Remove this code; the only loop should be inside the function, now …

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

When you call the function, pass it the list, `n`, as the argument, as follows …

``````print_list(n)
``````

`x` is not recognized outside of the function, as it has not been defined there…

2 Likes

Thank You sooo much n = [3, 5, 7]

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

i’m facing the same problem

1 Like

I am also facing the same problem. I don’t know why. Bug?

``````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)``````

EDIT: had to remove

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

And it let me pass.

5 Likes

yup. making it a comment is not enough. need to remove it.

1 Like

Hello!
Guys try this-’

``````n = [3, 5, 7]

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

print_list(n)
``````

It will work for sure. Cheers! 10 Likes

Yes, that worked, but I’m not really sure why 1 Like

Hi guys,

Try this to debug your code and understand (for those who don’t understand, how code is working?) So far, this online tool has cleared many of my doubts.

http://www.pythontutor.com/visualize.html#mode=display

Cheers!

Abhishek

6 Likes

So it mainly works because you’re calling the items in each list based on where they are in the list… Looks like you’re working through each item by calling its location (i.e. range is the first item in the list, and by setting the length to X, you can run each item based on the length of each of the meta lists you’re trying to comb.

HOWEVER, if you looked at the example code at the top of the left column, you could mimic it to create this response (which may be what they were aiming at):

``````n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

def flatten(lists):
results = []
for numbers in lists:
for items in numbers:
results.append(items)
return results

print flatten(n)``````

n = [3, 5, 7]

# Don’t forget to return your new list!

def double_list(x):
for i in range(0, len(x)):
x[i] =x[i] * 2
return x

print double_list(x)

what am I doing wrong?

As i saw it was a matter of deleting the code that was on page and start with this one

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

print_list(n)
``````

I wrote the same thing, but it isn’t working for me… "Oops, try again. The body of your function should not contain any references to ‘n’ " <----- That’s the error message I’m getting. Where am i going wrong? Thanks for that link! I sometimes have difficulty with programming because I like to be able to visualize how things work. This link you posted is PERFECT.

Thank you.

thanks for that tool bro… wish i found it earlier… i think if i finish this course i will start it all over again and i will use this tool from fresh start…

Can you tell me why it wouldn’t work with; return print_list(n) ?
Thanks!

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

Just change all of the n’s to x’s.

thx man you help me a lot I was struggling on this one

Or you can also try this:

n = [3, 5, 7]

def print_list(n):
for i in range(0, len(n)): #<== “x” is changed to “n” here
print x[i]
print_list(n)