Modifying each element in a list in a function


#1

Why is this not the right answer?

I get this error: Oops, try again. double_list([0, 1]) returned 2 instead of [0, 2]

And in console i get: [6, 10, 14], which is correct.

Thanks for your help in advance.
Brg


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

double_list(n)
print n


#2

You're not returning n which is what the error message is about.
Hint: try to use the return command instead of print. :slight_smile:


#3

You see something printed and it looks right to you, so you assume everything is right. That's the mistake you're making. Instead, you need to test for the described behaviour, in this case it's complaining about the return value of the function:

print 'the function returns: {!r}'.format(double_list([0, 1]))

#6

I tried with return command, and i still don't manage to get it work.
Thanks for the effort.


#7

Thanks for your help, but i don't understand it.
Can you maybe simplyify it.
Thanks for your effort in advance.


#8

Effort and figuring out what you need to know next is on you :wink:
You can't shift either of those onto others


#9

When describing the action of a program, always be specific about the related details.


#10

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

print double_list(n)

So far i understand this, but i don't have any clue what is missing.
Can you maybe explain it to me in layman's terms,


#11

It wants you to return the list that you manipulated, (the entire list), in your case, that would be returning the parameter.
The parameter is x.

here you return x[i], but maybe just return x?


#12

See @kyleaw 's post regarding what to include in the return statement.


#13

Thank you all for your kind support, i understand it now.
Brg


#14