Modifying an element of a list in a function


#1

I am at https://www.codecademy.com/courses/python-beginner-nzzVa/2/3?curriculum_id=4f89dab3d788890003000096

According to the instructions, I have to add 3 to the first index of list n. After modifying my code many times, I haven’t got it to work.


#invalid syntax
def list_function(a):
    return a[1] = a[1] + 3

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

#return function
def list_function(a):
    a[1] = a[1] + 3

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

#wrong output
def list_function(a):
    a[1] = a[1] + 3
    return a[1]

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


#2

i think this code is the closest:

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

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

the only mistake you made was to return the second item, you should return the whole list


#3

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