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.
is it because the function is run once, before the function is called so it is then is operating on the already stored list which is twice the original?
For loops seem to be a reoccurring death to me.I canāt seem to get comfortable with them. Anyhow - here is my new problem of the day.
CONSOLE:
[6, 5, 7]
That is my code. Why is it not doubling the whole list and only the value at index 0 in list n? I vaguely remember that a return statement forces the loop to stop so I imagine that is why. But if so, how do I get the modified list for all values? I tried indenting my return statement and received a syntax error so that clearly was not it.
Also with the code linked above codeacademy gave me the following prompt:
double_list([0, 1]) returned [6, 5, 7] instead of [0, 2]
I am deeply confused by that message and clearly donāt have a full understanding of for loops. I will go back to the lesson again but would love some input that may help me over this hump. Thanks.
Wow - just clicked on the solution. All I had to do was align my return statement with the for loop. Sigh - Iām not sure if that means I donāt have a grasp on return statements or for loops.
@ryan.hurley.btc I canāt replicate the error message double_list([0, 1]) returned [6, 5, 7] instead of [0, 2], but I can help with the other part of your question.
First, you need to change n to x in your double_list function. More specifically, right now your function is saying to expect an argument, x, but you are calculating the length of n, and returning n.
Lastly, you are correct that the return command is terminating the for loop. That means the loop is only getting through index 0 before terminating. So, a list of any size would only have the first value doubled.
To fix that, all you have to do is unindent the return x command so that it is at the same indentation level as the for loop, instead of being part of the for loop.
I got that same āout of rangeā error. It wasnāt until I actually asked for the solution, and then went back to my code, and compared the solution to my code, which was no different than in this forum. I noticed that in the solution, the variable x needs to replace ALL instances of variable n in that function.
So, the FOR statement would be:
for i in range(0, len(x)):