Modifying an element of a list in a function


#1

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

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

no idea why this is showing up as wrong. says invalid syntax and points to the equal sign on line 2


Modifying an element of a list in a function, question
#2

Hi @vital03,

A single statement cannot perform both an assignment and a return at the same time, therefore this is invalid ...

return x[1]= x[1] + 3

Instead, have a statement perform the assignment, then follow it with another statement that returns the object that is specified by the instructions.


#3

Thanks, now I'm getting a different error. the console shows 8 coming out but I get pairs of random numbers from the 'oops try again' pop up

here is the new code

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

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


#4

You have this ...

    return x[1]

... but the instructions state ...

Return the list.

You returned only one element of the list.


#5

Should have seen that. Thanks for taking the time @appylpye


#6