12 Printing out a list item by item in a function - error


#1

I dont understand why my code does not work. Here it is:

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

It returns an error message saying " It looks like you either didn't call print_list(n) or there is something wrong because 3 wasn't printed."

Any help is more than appreciated!


#2

Well you didn't call print_list, you created this function but so far you didn't use it. Also have a look at this:


As indents are part of the syntax in python it is important that you display code as code and not as plain text.


#3

Hi, thank you for your answer.
I tryied that but it didn't work either.

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


Printing out a list item by item in a function- error maximum recursion depth
#4

Sorry, I changed x for n and worked, I don't really understand why.


#5

As said see the link on how to format your code

```
code
```

And about the rest. Well what you enter at the () when calling the function is assigned to the variable in the () of the function definition. So in this case the list n gets assigned to the parameter x. Also you don't need a print before print_list as it doesn't return a value and has its own print statement inside.


#6

Hi, thank you again.
I was trying to follow the instructions of the exercise since I have difficulty with programming. I actually don't understand much of what I do but I'm trying.
Thank you for your help.


#7

If something is unclear you can for example test it here:
https://repl.it/languages/python3
or see how it is executed here:
http://www.pythontutor.com/visualize.html#mode=edit
or just ask questions here.


#8

Hi I had the same issue, the function wouldn't take "x" or "num" or whatever else I could think of as an argument. In the end I tried the "v" and that worked out... The instructions were somewhat misleading for this particular exercise. Instructions are usually remarkably clear otherwise.


#9

Thank you for these links! They are very helpful for double checking that I actually know what's going on with the code.


#10

Hello guys, I don't know what wrong in the following code:

Please can somebody help me on this


#11

Even i had the same issue and this worked for me

n = [3, 5, 7]

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

print_list(n)


#12

This is what what I did and it worked

n = [3, 5, 7]

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

print print_list(n)


#13

I don't know what kind of problem you can call this but after making my code shorter it kind of worked. What I did is (as you can see from the screen shot that I posted earlier) I deleted the commented part from the code from line 2 to 4 and thus reduced the length of the code to 6 lines. What a frustrating thing. Even though the code was correct it didn't used work. At last I got it correct. So I don't think there is a need to change x to v or num or anything else..


#14

thank you, you helped me figure out a simple error i made in my program.


#15

you probably just have to delete the commented lines.


#16

I had the same issue.. no change to the code I wrote, just had to remove the existing comments, which shouldn't have any affect at all.


#17

That is because you didn't really call that function to be used.
Your code should look like this:

n = [3, 5, 7]

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


#18

I tried commenting the "scaffolding" out and was still getting the "the body of your function should not contain any references to n" error. Deleting that scaffolding fixed it. This lesson is bugged.


#19

That did it for me, it is a bug. Thanks


#20

you are missing calling for print for print_list(n)