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


#1

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


#2

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..


#3

Thank You sooo much :grinning:


#5

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


#6

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)

#7

EDIT: had to remove

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

And it let me pass.

Source.


#8

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


Try it out 12/18
#9

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! :smile:


#10

Yes, that worked, but I'm not really sure why :expressionless:


#11

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


#13

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[0] 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]]
# Add your function here
        
def flatten(lists):
    results = []
    for numbers in lists:
        for items in numbers:
            results.append(items)
    return results

print flatten(n)

#14

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[1]

print double_list(x)

what am I doing wrong?


#15

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


#16
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? :open_mouth:


#17

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.


#18

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..


#19

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)

#20

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


#21

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


#22

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)