10. Modifying an element of a list in a function


#1


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

Change list_function so that:

Add 3 to the item at index one of the list.
Store the result back into index one.
Return the list.

Without adding any code, I hit 'save & submit.'

"Oops, try again. list_function([0, 4]) returned 4 instead of [0, 7]"

Repeat.

"Oops, try again. list_function([2, 4]) returned 4 instead of [2, 7]"

Repeat.

"Oops, try again. list_function([1, 1]) returned 1 instead of [1, 4]"

et cetera

Why does the error message change when the code remains untouched?


def list_function(x):
    return x[1]

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


#2

I'm guessing that codecademy has setup the scoring engine to test the code with a random number that way you can't tailor your code to the one input it's going to test with.


#3

same problem here, any tips to fix it?


#4

We have to return the List not the element (after updating it). So

 return x  (but not x[1])

#5

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