Your function caused the following error: list index out of range


#1

this is my code, from what I saw of other people’s code, it should work, but it still says: 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

Hi @codewhiz78307,

Within the print_list function, you are referring to the global variable n here …

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

… where you should be referring to the function parameter x.


#3

ah, thanks, that worked (also, since you’re a mod, in the previous exercise, list manipulation in functions, it says lst instead of list)


#4

In example code, lst is sometimes used as the name of a variable that refers to a list, as is the case in Exercise 11: List manipulation in functions. It is almost always a bad idea to use the name of a built-in function as the name of a variable. list is the name of a Python built-in function that can be used to convert certain other data types into a list. Therefore, it is preferable to use the similar name lst, instead, as the name of a variable within example code that is designed to demonstrate how to work with a list.

See Python 2: Built-in Functions: list.


#5

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