"13/18 Modifying each element in a list in a function" Error Message


#1



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

I don't understand this lesson I am on. I have the right "for" function, don't I? I keep getting this error message: "Oops, try again. double_list([0, 1]) resulted in an error: list index out of range" Any help on this will be greatly appreciated, thanks guys!


n = [3, 5, 7]
def double_list(x):
    for i in range(0, len(n)):
        x[i] = x[i] * 2
# Don't forget to return your new list!
    return double_list

print double_list(n)


#2

this bit of code:

len(n)

should be len(x), the exercise is going to test a few lists (including lists which have a length of 2), and currently you are checking the length of the list you created on line, rather then the list passed into the function


#3

I changed my code with code and here is what I have:

n = [3, 5, 7]
def double_list(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
# Don't forget to return your new list!
    return double_list

print double_list(n)

It is now giving me this error message:
"Oops, try again. double_list([0, 1]) returned instead of [0, 2] "

Does it have something to do with my "return" part?


#4

yea, it indeed has to do with the return. Your return the function (double_list), you want to return the updated list (x)


#5

Ah thank you! I have finally figured it out:

I just had to change it to

return x

#6

Hi .. I am fairly new to programming. Can you please tell me why the length of the list has to be 2 ?


#7

If coded correctly, the function should be able to take a list of any length. (since you check for the length of the list which you pass into the function)


#8

thank you ! It is a bit more clear to me now.
I will keep working on it over and over again !