Modifying an Element in a list of a function


#1



My code is generating the correct output, but I am receiving the following error code:

list_function([5, 4]) returned 7 instead of [5, 7]


I don't know what this means or how to correct this error.

Thank you for your help.


def list_function(x):
    x[1] = x[1] + 3
    return x[1]

n = [3, 5, 7]
list_function(n)
print n


#2

you need to return the whole list, not just the second item of the list


#3

But, when I ran it on my PC. It ran good. Idk why.
Same code like him/her.


#4

there are no syntax problems with the code, the problem is that you return the first item in the list, while you should return the whole list


#5

you are returning only one element in the list with your function

return x[1] should be just return x

then just print the function with respect to the list n


#6

one way that helped me to grasp this was to understand that the print statement is working for the function. It is going to print what is returned by the function, at least for this exercise. The exercise instructs you to print the whole list, not just the first element. by only returning x[1], you are telling the print statement to only print x[1]. As people mentioned above it, you should return the whole list with just 'return x'


#7

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