Modifying each element in a list in a function - list index out of range error


Oops, try again. double_list([0, 1]) resulted in an error: list index out of range

n = [3, 5, 7]

# Don't forget to return your new list!

def double_list(x):
    for i in range(0, len(n)):
        x[i] = x[i] * 2
    return double_list(n)


You are attempting to get the length of a list, n. However, inside the function, it does not know what n is.

The function will use the placeholder x until you call the function itself on n, at which time it will replace the x throughout the function with n.


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