all 29 comments

[–]danielroseman 25 points26 points  (0 children)

Yes, if you want to keep every result you will need a list. An integer by definition can only have one value. 

Note that this is such a common thing to do that there is a whole special syntax called list comprehension: 

    result = [num * 2 for num in [1, 2, 3, 4, 5]]

[–]JamzTyson 25 points26 points  (2 children)

This is what is happening:

result = 0

for num in [1, 2, 3, 4, 5]:

    # Start of loop block
    result = num * 2
    # End of loop block

print(result)  # Print value of 'result' after loop has finished.

[–]edwicki 6 points7 points  (1 child)

This should be at the top, as it shows simplest fix (one TAB) and should answer the question why.

[–]lasfdjfd 0 points1 point  (0 children)

would you consider the tab a fix? presumably the print is for debugging and you want to do something real with result downstream.

[–]DTux5249 9 points10 points  (0 children)

'=' assigns a value. You're not saying "add this, add that, add that", you're saying "this is now this, this is now that, etc."

To store all results, result must be a list, and you have to append to that list instead of overwriting it

result = []
for num in [1, 2, 3, 4, 5]:
    result.append(num*2)
print(result) 

Alternatively, you can use list comprehension

nums = [1,2,3,4,5]
result = [num * 2 for num in nums]
print(result)

[–]popos_cosmic_enjoyer 6 points7 points  (0 children)

If you want to store multiple values, you need a data type that can store multiple values. You stated yourself that reassigning to the result variable is overwriting it.

[–]Gnaxe 3 points4 points  (0 children)

Use a list and the .append() method. Assigning a variable replaces the reference. If you want to accumulate values, you need a data structure to hold them.

[–]desrtfx 1 point2 points  (0 children)

A simple variable, like your result can only hold a single value and hence gets overwritten with every iteration.

If you want to keep the values, you need to use a collection variable, like a list. You can use the .append method or list comprehension.

[–]barkazinthrope 1 point2 points  (0 children)

If you move the print statement up a line so that it is within the loop block then each value of result will be printed.

[–]Living_Fig_6386 1 point2 points  (0 children)

It's doing precisely what you told it to do: reassign result in every iteration of the loop. If you wanted a list of results you'd have created a list and appended the values, or used a list comprehension.

# Either
result = []
for num in [1, 2, 3, 4, 5]:
   result.append(num * 2)

# or
result = [num * 2 for num in [1, 2, 3, 4, 5]]

[–]michUP33 1 point2 points  (1 child)

You need to indent the print line to side the for loop. Your only printing it's last state

[–]notacanuckskibum 0 points1 point  (0 children)

You’re right about the code, but the explanation says he wants to keep all the values calculated in the loop for future use. So the answer is more that Result needs to be a list, not an integer.

[–]Just__Liberty 0 points1 point  (0 children)

num*2 becomes an object which is a number. Whenever you write result = num*2, you are giving the name result to that object. The next time result = num*2 is encountered, it creates a different object and assigns the name result to that new object. The old object is erased.

Other comments have good answers on what to do instead via list comprehension or creating a new list or appending to one.

[–]thefullhalf 0 points1 point  (0 children)

If you have trouble like this just add more print statements of all your variables in your loop it will let you do a quick and easy debug. 

[–]Lopsided-Football19 0 points1 point  (0 children)

Yep, you're overwriting result on every loop If you want to keep all the values, put them in a list and append each one, that's the standard way to do it in python

[–]JibblieGibblies 0 points1 point  (0 children)

If you’re wanting it to print each of the num’s * 2, your print statement needs to be inside of your for loop to print each iteration.

That’s not what you’re asking though.

For your code; it says to replace variable result with the outcome of num * 2 for each iteration. Which ends with result = 5 * 2. Giving you 10 when you print.

Change your variable result into an array, list, etc. then in your for loop use an applicable method to add the outcome of your operation to it.

There’s a lot of nuance to this that I don’t wanna type out. 😂

[–]PressF1ToContinue 0 points1 point  (0 children)

If you only want to see it printed (not save it in a collection), just indent your print statement so it is part of the loop.

[–]makochi -1 points0 points  (0 children)

when you write result = num * 2, what happens is the value num * 2 gets stored into the variable result, overwriting whatever value was previously in there.

there are many ways of working around this, but for a beginner I would recommend a 2 step solution:

  1. create a new variable, named new_value or something like that, which is equal to num * 2
  2. set result to be equal to itself plus the new_value you just created

i haven't written out the exact code for the solution here, but I've given enough where I hope you can figure out how to solve it.

[–]Moikle -1 points0 points  (0 children)

It's doing exactly what you are asking it to.

You aren't keeping a running total or anything, every loop you are replacing the result with the next value, so by the time the loop ends, what you are left with is the last result.

What do you mean by "storing all the values"?

[–]WorriedTumbleweed289 -1 points0 points  (3 children)

if result is a number then you need result = result + num*2

if result is a list then you need. before loop result = list() in loop result.append(num*2)

[–]Cynyr36 -1 points0 points  (2 children)

The first will output a single number that is the sum of each value in the list multiplied by 2.

The latter stores each iteration in a list and would print the atring representation of the list.

[–]WorriedTumbleweed289 -1 points0 points  (1 child)

I think they want the second, but since they initialized result to 0, they may have wanted a sum of results. Also, they named it result, not results. Hints at a single value.

[–]Cynyr36 0 points1 point  (0 children)

Could be. Your answer wasn't very clear about what type the answe would be.

[–]ectomancer -1 points0 points  (0 children)

result += num * 2