Please help.says the index is out of range


#1

n = [3, 5, 7]

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

#2

We have an array n containing the numbers 3, 5, and 7
It's total length thus is three.

Therefore the range of (0, len(n)) is (0..3)

Because arrays start indexing at 0
When we try to print n[3]

There is nothing to print! You've indexed out of range!
Your array only contains n[0],n[1], and n[2]


#3

what do I print then. I am still very confused.

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

#5

could you maybe give me more of a clue. this level makes no sense to me. what does range mean anyway


#6

from https://www.codecademy.com/articles/glossary-python

The range() function returns a list of integers, the sequence of which is defined by the arguments passed to it.

(For syntax and examples please look it up in the Codecademy Glossary by clicking the link above.)

And also, to understand more about it, go to this part of the course:
https://www.codecademy.com/en/courses/python-beginner-nzzVa/3/3

Oh, and please give a link to your course. It would really help us to help you with the problem.


#7

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


#8

Okay, I've made a big mistake – now that I've checked my own code.
print range(0,3) will result in [0, 1, 2].
I let fight_dragons' reply confused me. So I've deleted my first reply to you. Terribly sorry for making things difficult.

Now let's start fixing your code.

n = [3, 5, 7]

for i in range(0, len(n)): #you can remove this code, because it's just an example given to you by the course to help you.
    print n[i] #delete this too. you don't need it anymore.
    
    
def print_list(x): #now let's see your code!
    for i in range(0, len(n)): #this is your problem. it should be len(x) instead of len(n) because the argument of this function is x.
        print x[i]

print_list(n) #I've added this code for you to check if our code is correct.

Check the course's "Stuck? Get a hint!". It should be this simple!!!


#9

thanks so much. all I really had to do was delete the example and It never said that in the instructions. finished it now anyway. thanks for the help


#10

Terribly sorry for wasting your time with my stupid answers!

But hey, I'm glad you sorted this up.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.