Why is my return value not correct?

Question

Why is my return value not correct?

Answer

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
3 Likes

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.

2 Likes

The syntax error is caught by the Python interpreter, not CC. We cannot write a statement as a return value. The = sign is not expected there.

5 Likes

My code does. what is in the description, but I got an error message. Why?
Or did I misunderstand something?

2 Likes

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.

2 Likes

Thank you, that clarifies it for me.

4 Likes

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?

I just hit the Run button on the same code that the “Solution” button spat out, and the result I initially got popped up: [3, 8, 7]

<shoulder_shrug.exe>

ya i got the same. mine came out the 3 8 7 (in proper format but it said it was wrong. I have no idea what happened.

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.

1 Like

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) :arrow_up:

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:

1 Like