The instructions ask for the entire list to be returned, so if you’re only returning a single modified element, that is the issue. Instead, what we want to do is modify the element, and then on the next line write our return statement that returns the entire list x. Like this:
def list_functions(x):
# Modify the single element here
# Return the entire list here
After reading the above answer, I realized my mistake and managed to solve the exercise, but what I don’t understand is why Codecademy was identifying the mistake as a syntax error and the little arrow was pointing towards the = sign? Unless I’m misunderstanding and the little arrow actually points to the whole line of code rather than a particular column.
I would recommend testing your code with a different list maybe the one that is stated in the error comment. After n create a new list m = [3 , 6] and print (list_functions(m)). You would expect [3,9], but you don’t get that.
Hopefully you’ll be able to identify what is wrong. A hint is it’s in your function list_function.
The “solution” in the interface doesn’t make any sense. The prompt asks specifically for 3 to be added to index [1], then the result stored in index [1]. When I ran my code, it did exactly what was asked, but for some reason the interface wanted index[1] to be deleted instead of having the novel value stored there. But when I ran the “solution” code, index[1] had been deleted. Is there an error in the interface reading this code improperly?
My mistake was that in my function i was returning n instead of returning x. The result is exactly the same but I think its not what the test wants so it rejects it. If anyone else is having that issue make sure you return ‘x’ and not return ‘n’ and hopefully it’ll accept it.
When should you use n instead of x. Im a bit confused as in previous lessons it seems as they interchange.
And I’m not sure why using n instead of x here causes an error
What is your error?
Not sure what lesson this is connected to…but is this the code? (I followed the linked topic in the OP)
def list_function(x):
x[1] = x[1] + 3
return x
n = [3, 5, 7]
print(list_function(n))
[3, 8, 7]
In this example code, x is a local variable and is only available inside the function. n is a global variable and is available outside the function scope.
Perhaps this explanation (from the other topic linked above) of scope will help: