Lists and Functions: Printing out a list item by item in a function


#1



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

can some one tell me whats wrong with this code
it is showing me the error 
Oops, try again. Your function caused the following error: list index out of range


#2

lets see what if we want to print the items of another list:

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

ouch, we get an IndexError

make sure your function works for any list of any length


#3

I think it s when you say range(0, length(n)). It should have an x instead of n. This way, it works for any list of any length.


#4

I know, but i wanted to digitalpro15694 to reach this conclusion itself, if that didn't happen, i would explain something more about function parameters and arguments


#5

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