13/18


#1

"""
n = [3, 5, 7]
def double_list(x):
for i in range(0, len(n)):
x[i] = x[i] * 2
return x
print double_list(n)
"""
the error i get is: Oops, try again. double_list([0, 1]) resulted in an error: list index out of range
whats wrong with this code?


#2


#3

on this line

for i in range(0, len(n)):

you are using the length of the list n you created outside the method which means that no matter the list you pass the range will always be form 0 to 2. to correct it you have to do

for i in range(0, len(x)):

#4

i've done that and now the error is Oops, try again. double_list([0, 1]) returned [0, 1] instead of [0, 2]....i really cant figure out whats wrong with this piece of code


#5
n = [3, 5, 7]

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

print double_list(n)

this worked for me.
the len(x) seems to be only problem in your case as rydan previously mentioned. try this and report back. hope u figure it out


#6

yay it finally worked with your code. thanks!


#8

Mine keep saying double_list not defined help!

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


#9

Your code is correct!
But you have to take care on the indentation:
for i in range(0, len(x)):
--------x[i] = x[i] * 2
return x

FOR and RETURN have to be on the same indentation level!!

Hope this is helpful :slightly_smiling:


#10

Thanx this was helpful. But there is still one thing I don't understand quite well..
Why has the FOR and Return to be on the same indentation?


#11

I had the same question. If I'm understanding this thread (link below) correctly it's because the for loop will continue forever unless you tell it to end by using the return. Because return is directly related to for it needs to be on the same 'plane' or indentation.
(https://discuss.codecademy.com/t/unable-to-understand-the-use-of-return/4929/2?u=srodr008)