Modifying each element in a list in a function


#1


n = [3, 5, 7]

for i in range(0, len(n)):
n[i] = n[i] * 2

Don't forget to return your new list!

print double_list(n)

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

print double_list(x)

Hi,
The above is my code, and no matter what I try to do to fix it, the console keeps telling me:
Syntax Error: 'return' outside function

Please help. Thanks!


Replace this line with your code.


#2

yeah because len(x) will return 3 , and range will be from 0 to 3 ( 4 items in list )
your list has 3 items (from 0 to 2 ) not 4 items so you can correct that by range(len(x)) or range(0, len(x)-1)


#3

Have you correctly indented? Sounds like you may have the 'return x' sitting outside the function.


#4

Just like @chipplayer37926 mentioned, it seems your return statement's indentation is not correct.
Try copying and pasting your code into online IDEs such as repl.it so you can fix it, test it and bring it back to Codecademy.

@azazaz:

His code is correct, when we are talking about the use of the range function. Whenever we use range() it means we want our code to generate a list starting from 0 until N - 1 (where N is the value you passed as a parameter to the function).
You can also see the range function like this range(stop) or range(start, stop, step) (the first option is the default one, if we can say it like this) so doing range(3) or range(0,3,1) will result at the exact same thing: a list containing numbers from 0 to N - 1, in this case 0 until 2, inclusive.
The way you did wouldn't work because the value 3 is an integer and such has no len(), you can test it if you want.
If you already know the length and need to generate a list, simply use range(valueHere).
Hope my explanation was clear, if not, I'll find another way to explain!


#5

Not really, so what's the solution or answer since nothing seems to work


#7