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


#1

Stuck, not sure why. I got this code down:

n = [3, 5, 7]

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

def print_list(x):
    for number in x:
        print number

print_list(n)

Where the original coding of the lesson sits on top, while the bottom part is what I added.

But then I get this error when I try to submit it:

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

I don't get it, my code doesn't? What's going on?


#2

Did you mean to write,

n = [3, 5, 7]

?


#3

That's what I see? I didn't change anything on that part of the coding, it was what came with the lesson.


#4

Oh, yeah, sorry, I forgot to copy the very beginning part.


#5

Change all the 'n's to 'x's

x = [3, 5, 7]

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


#6

Oh, Thanks for that! It really helped! Odd how you have ton change ALL of them to x.:upside_down:
Feels like cheating the system, but somehow it's not.:sweat_smile:


#7

It's only cheating yourself.


#8

It worked...

You've got to be kidding me. I mean, I am not even the one who set the thing as n, it was part of the exercise!

Stupid freaking exercise.

You know what's funny? I tried testing my coding on the Python compiler, and it runs smoothly, and exactly how it's supposed to run, so I knew I wasn't wrong.

Thank you for this.


#9

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

this code gives me error saying "did you create a function print_list()"
and in console is says " File "python", line 6
print number
^
IndentationError: expected an indented block"

Someone help


#10

Curious, but I am getting list index out of range with this:

n = [3, 5, 7]

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

Can´t see why, any ideas?


#12

the exercise will test multiply list by calling the function, so you get the length of the list passed into the function (len(x)) not the length of the fixed lists you created (len(n))

x is the function parameter, it is a variable inside a function, it will hold the argument passed into the function, as you can see here:

def example(y):
   print y
example([1,2,3])

#13

this my code and i keep getting the same error. no idea why


#14

on line 7, the line is not properly intended. More indention is required


#15

Thanks, I will keep that in mind.