Index out of range, 12/18


#1

There seems to be a problem with the following code:
n = [3, 5, 7]
def print_list(x):
indent preformatted text by 4 spacesfor i in x:
indent preformatted text by 4 spacesindent preformatted text by 4 spaces`print x[i]
print_list(n)

In the console I get "index out of range. ". The purpose of the excersise is to learn using range in for loops, so when I put range instead of 'x' in the for statement, the code has no problem. But what is the mistake in the above code and what is the meaning of "index out of range? And please inform me of how to format the code text in the forum , so as the indentation can be seen by other users, since it's my first time posting here.Thank you in advance for your response.


#2
n = [3, 5, 7]
def print_list(x):
      
    for i in x:         """ here according to your code **i** refers to the elements
         print x[i]          in list x i.e 1,2,3  and then in  x[i] , i also takes the value 
                               3  which is out of range in the given list  """
        
print print_list(n)

#3

n = [3, 5, 7]

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

print print_list(n)