Why is double_list returning the wrong list values?


#1

Question

Why is double_list returning the wrong list values?

Answer

Assuming you wrote the rest of the function correctly, using x where n is in the for loop the exercise starts you off with, then the issue is likely how you indented your return statement. Remember, as soon as a function executes a return statement, it exits immediately and gives that value to wherever the function was called in the program. Take a look at the code below:

def my_function(numbers):
  for i in range(0, len(numbers)):
    print numbers[i] = 0
    return numbers

The return statement is inside of the for loop, and therefore the loop can only possibly run one time before the function exits. To fix this, unindent the return statement until it’s at the same level as the for, so it’s still inside of the function.


FAQ: Learn Python - Lists and Functions - Modifying each element in a list in a function