Modifying each element in a list in a function


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/2


I'm getting an error: Oops, try again. double_list([0, 1]) returned [0, 1] instead of [0, 2]


What exactly am I doing wrong? The output on the terminal is right! [6, 10, 14]


n = [3, 5, 7]

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


print double_list(n)


#2

Hi @ricardot66,

Here, you are accessing the global variable, n, instead of accessing the function parameter, x ...

        n[i] = n[i] * 2

Although you may have gotten the correct result in the terminal for double_list(n), your code only works for n rather than for any list of numbers that is passed to the function. Therefore, when Codecademy tests your code with another list of numbers, it fails the test


#3

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