Help with 7.13: Unsure why this doesn't work


#1

https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/modifying-each-element-in-a-list-in-a-function?action=resume_content_item

Here is my code:

n = [3, 5, 7]

def double_list(x):
for i in x:
i *= 2
return x
print double_list(n)
My output looks correct:

[6, 10, 14]

But codeacademy says it is wrong. It says:

‘double_list([0,1]) returned [0,1] insted of [0,2]’

I tried it in my console, and sure enough, it does return [0,1], which is wrong. But I don’t understand why. If it works for the larger numbers, why doesn’t it work for [0,1]?

(Also I know that my code is not indenting here…not sure why that is either because it is indented correctly as I am typing)


#3

Your code…

>>> n = [3, 5, 7]
>>> def double_list(x):
	for i in x:
		i *= 2
	return x

>>> print (double_list(n))
[3, 5, 7]
>>> 

When iterating values, they are read only, so cannot be changed in this form of loop. We need to access the indexes so that the elements can be modified.

>>> def double_list(x):
	for i in range(len(x)):
		x[i] *= 2
	return x

>>> print (double_list(n))
[6, 10, 14]
>>>