Out of range index



My code threw the error: list index out of range

I expected my code to print the list n = [3, 5, 7] twice (which it did) without error.
My code

n = [3, 5, 7]

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

I would love to know why codecadamy is being a dick this time.


I simply don't quite understand what the exercise program is really doing behind the scenes, because frankly, I reset the whole code, run it once to make it fail, and then left only the list on top, and it also gave the same results of

error: list index out of range


I also did this just for fun after completing the exercise and saw your predicament

n = [3, 5, 7]

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

print_list (n)

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

and CA says: > Oops, try again. The body of your function should not contain any references to 'n'


Anyway, I think CA is looking for a certain type of coding for this particular exercise, otherwise it will overflow. I can't imagine why yet, unless there's someone more in the know to explain to me, or I get to see CA's particular coding for this exercise...


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