Modifying each element in a list in a function


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

I'm not understanding where the error is. The error message I keep receiving is "Oops, try again. double_list([0, 1]) returned [0, 1] instead of [0, 2]".

Any recommendations would be greatly appreciated.


a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the loop will break in order for the function to be able to end

so your loop breaks in its first iteration, is this desired?


As suspected by @stetim94, there is too much indentation on the return statement.


I see, since it was over indented it caused the the function to break prior to what was requested to run by the lesson. Correct? I just want to make sure i'm thinking about the request properly when it comes to analyzing what's being requested.


Thank you! I greatly appreciate the insight.


yes, exactly


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.