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


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/1?curriculum_id=4f89dab3d788890003000096

The code prints out each item line by line, followed by
None
None
For some reason it throws the error message "list index out of range", even though it prints all the items in the list

Can anyone please tell me why my code doesn't work, or is it a bug?

 n = [3, 5, 7]

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


#2

Try the following, and watch what happens:

print_list([2, 3, 4, 5, 6, 7, 8, 9, 10])
print_list([2])

#3

It gives the same error message


#4

Not for the first one.

And the second one shows you that your function indeed doesn't do what it should.

You appeared to be confused about why your function appeared to work but codecademy said otherwise. Here, I gave you cases where your function isn't working so that you can see for yourself.


#5

They both give that error message. I tried them by themselves and together.


#6

No. The first one does not trigger that error message.

You can remove both and you'll get that error message from codecademy. But the first one isn't what causes it.

Look at how your function behaves differently from what it is supposed to do for those calls.


#7

When I do the first one by itself, at the bottom of the screen it gives the same error message, but in the console it says that i gave too many arguments.


#8

Your function takes one argument and both my calls have one argument. Perhaps you copied them incorrectly by hand instead of using your OS's clipboard feature. Or perhaps you have some other function call that triggers that error.


#9

Sorry I made a mistake in the copying, the first one works now? But why does it still say i have an error at the bottom of the page, even if the list at the beginning prints in the console?


#10

Does it work? How many numbers are printed? It should print them all, right? That's the purpose of the function

Your observations are all over the place, you can't draw any good conclusions from them if they are not accurate!


#11

OK so for some reason when I run the first line with 9 integers, it only prints the first three.


#12

Correct.
And what happens if you print the first three values of the second one?


#13

Sorry, I don't understand. In the second one, there is only one value, right?


#14

Yeah. So what happens if you print the first three?


#15

print_list([2, 3, 4])
You mean like this?


#16

no, the first three of [2]

my_list = [2]
print my_list[0]
print my_list[1]
print my_list[2]

What happens


#17

I just face palmed so hard. I realised that I forgot to change n to x in my original code. I feel like such an idiot.


#18

And we're gonna make mistakes like that all the time.

Sometimes we can be extra careful, sometimes we can review small amounts of code for its correctness.

Other times, we need to be able to systematically find where the problem is.


#19

Thank you so much!:joy:


#27

This topic is solved.