Oops, try again. Your function caused the following error: list index out of range


#1



Oops, try again. Your function caused the following error: list index out of range


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


#2

@leo.ssu,

++++ reset of session *****************
under certain circumstances you can shoot your Browser in an
inconsistent state.

Therefor it is of an advantage to know that you have 2 reset facilities:

One is the use of the F5-key which does a refresh Browser

and

Two, select&copy your code
Then use the Reset Code button of the course-window,
then paste your code back in.

Addendum
General Notes:
Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).
CTRL 0 to reset browser zoom


#3

Here len(n)=3; So when n=3 in for loop x[3] will show error.
use for i in range(0,len(n)-1).


#4

Try this:
for i in range(0,len(n)):

instead of this:
for i in range(0, len(n)):


#5

@connecttopawan: That is not how the range function works. Range always excludes the ending value. So range(0, 3) gives [0, 1, 2].


#6

@karatechap: The space between function arguments makes no difference...


#8

Hi @leo.ssu ,

Your function has a parameter named x, as specified in its header ...

def print_list(x):

... but inside the function, your are accessing the global variable, n ...

for i in range(0, len(n)):

When Codecademy passes your function a list that is shorter than n, as part of its testing process, the list index goes out of range, because you are using len(n) as the ending index. Use len(x) instead.


#9

Thank you! you are right, I figured it after i posted this