10. Modifying an element of a list in a function



I can't think of a reason why the code beneath this text wouldn't work, however, for some reason, Codecademy keeps giving me an error message. The error message is:

File "python", line 3
SyntaxError: can't assign to operator

I'm certain that this code should define replacement, and then add replacement into the list in the place of the second item (or first, based on how Python looks at items in lists).

def list_function(x):
    return x[1]
    x[1] + 3 = replacement
    x[1] = replacement
n = [3, 5, 7]
print list_function(n)


It seems that you reversed the variable with the assignment on line 3.
Using this:

Instead of this:
replacement = x[1] + 3

The computer is thinking that you're trying to make 'x[1] + 3' a variable.

The return statement is also causing problems with it being before the process of the function and it is returning the index of the list rather than the full list.

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


Thank you!:grinning:


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