Why is double_list returning the wrong list values?

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.

10 Likes

My code is working, and I am getting a tick as if it is correct but the console is showing 12,20,28 which is 4 times the input not twice…?
Why?

2 Likes

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?

3 Likes

Hi grogle,

I ran into the same problem and realized that the initial for loop was the culprit. Delete or comment out the first loop and run your code again.

2 Likes

Hello all,

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.

3 Likes

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.

2 Likes

@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.

Good luck!

4 Likes

My code looks like this:

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

Looks okay to me and it prints [6, 10, 14] but I still get the error message
“double_list([0, 1]) resulted in an error: list index out of range”

pls help

the exercise did additional validation by calling the function with [0, 1] as argument, lets add this function call to see what is happening:

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

we can indeed see the index error, any idea how that could be? Your function should be handle lists of different lengths.

2 Likes

I have a question:
Can you also do like this?: (my code works though as it prints [6, 10, 14] )

n = [3, 5, 7]

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

print double_list(n)

instead of placing a new variable ‘x’ into the function and for-loop?

Having the same name for a global variable and parameter is generally not a good idea

n is is also just one list, a function can easily handle multiple list:

y = [2, 4, 6]
n = [3, 5, 7]

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

print double_list(n)
print double_list(y)
2 Likes

Oh ok, got it! Thanks!

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)):

That is quite a difference. My code sample was only to demonstrate/highlight the problem.

Its important to understand why you need to use the function parameter (x), you understand? :slight_smile:

You know, @stetim94, just when I think I understand that part, the next step proves me wrong, :neutral_face: :upside_down_face: :neutral_face:

1 Like

Yea, programming can be frustrating. Taking your time, persistence pays off

What question do you have for me?

1 Like

Hey - when i click on ‘view solution’ - it showed me an incorrect solution.

When i click on ‘replace with solution’ - it showed the correct solution.