Modifying each element in a list in a function


#1

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.


#2

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?


#3

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


#4

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.


#5

Thank you! I greatly appreciate the insight.


#6

yes, exactly


#8

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