I don't understand what is wrong with this code


#1

n = [0, 2, 4]

def print_list(x):
for i in x:
print x[i]
print print_list(n)

Shouldn’t the for loop print the number in index 0, then index 1, then index 2.
So shouldn’t it print
0
2
4


#2

You will not need to print since the function does that.

This is a look-up loop that retrieves values from the array. i is not an index. All that will print is 0, and 4 (if it does not throw an error) since 4 as an index will be out of range.

for i in x;
    print i
for i in range(len(x)):
    print x[i]

#3

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