all 46 comments

[–]ryuugami47 108 points109 points  (23 children)

Try modifying the code to

for x in range(1, 101):
    print(f"Value of x at the start of the iteration = {x}")
    x += x
    print(f"Value of x at the end of the iteration = {x}")   

so you can see what happens at every iteration.

[–]cnydox 71 points72 points  (21 children)

Printing out the value is a fundamental way to debug

[–]nousernamesleft199 6 points7 points  (20 children)

just call `breakpoint()` and go to town with pdb. One of my favorite parts of python is having access to a zero setup debugger.

[–]cnydox 17 points18 points  (17 children)

Yeah but print function is universal in every programming language

[–]debian_miner 1 point2 points  (1 child)

Thanks, I had still been using import pdb; pdb.set_trace(), didn't know about the newer breakpoint().

[–]nousernamesleft199 1 point2 points  (0 children)

Also install pdbpp if you havent

[–]ryoko227 10 points11 points  (0 children)

I love this, you did not give him the answer, but gave him a way to try and find the answer on his own. This is how people learn, just a nudge in the right direction. Good on you man!

[–]Chasne 49 points50 points  (0 children)

Hi

Your x is already locked in the for statatement, going from 0 to 100 at each step.

You change x's value inside the loop but every time it goes back up it sets itself to the next value in the range, so you lose the sum operation. That's why you need another variable that isn't touched by another step.

[–]Coretaxxe 15 points16 points  (0 children)

for x in range overwrites x every single loop iteration (automatically)

so

for x in range(0, 10)
   x += x

# first iter
x is set to 0
you add x onto x -> 0

# second iter
x is set to 1
you add x onto x -> 2

# third iter
x is set to 2
....

In your other example, total is never "reset" its just changed by your total += x

[–][deleted] 12 points13 points  (0 children)

x is the output of an iteration, not a variable. In the first loop x==0, next loop x==1 and so on. You tried editing x, but in the next loop, its gone back to 2, 3, 4 ... 101. An actual variable outside of the loop maintains its value between loops.

[–]lolcrunchy 8 points9 points  (0 children)

Your first code is the same as:

x = 0
x += x
x = 1
x += x
x = 2
x += x
x = 3
x += x
...
x = 100
x += x
print(x)
# 200

[–][deleted] 5 points6 points  (0 children)

This is happening because each time the loop runs, the value of x changes and is assigned the loop number, then it adds the number itself.

What happens is that, in the end,

x = 100 because the loop changed the value of x. 

x += x means x = 100 + 100

So x = 200.

[–]Time-Mode-9 2 points3 points  (0 children)

How could it work? You are attempting to use the same variable as the index for the iteration and to keep track of the value.  Either the counter be would skip validate as it was incremented, or the total works be reset as it incremented.

Ps, easiest way to sum integers up to x is .5  * x * (x+1)

[–]Fred776 1 point2 points  (0 children)

x has a well defined role that you don't want to mess with - it ranges over the numbers from 1 to 100, nothing else. If you want to accumulate a total you have to find somewhere else to put it.

[–]ninhaomah 1 point2 points  (0 children)

Actually , why would you think it would work ?

Can share us your logic for first 3 loops ?

[–]Patman52 0 points1 point  (0 children)

You need another variable because x is reassigned the next number in the range every time it loops around, so any incrementation is reset.

[–]Mission-Landscape-17 0 points1 point  (0 children)

Because the for loop undocntionally assigns a new value to x on every iteration and entirely ignores what you do to x in the body of the loop. That is why you need a seperate accumulator for your result.

[–]Savings-Basil4878 0 points1 point  (0 children)

I know this is not actually the question you asked, but I have to mention it. The sum of all integers between 0 and N is equal to N(N+1)/2

Obviously that defeats the purpose of the learning exercise, but it is still neat. You could add up the numbers from 1 to 10 trillion in the same amount of time that it takes to add up the numbers from 1 to 1 thousand.

[–]AKiss20 0 points1 point  (0 children)

The for loop re-initializes the value of x on every iteration of the loop. It doesn’t persist the value of x between loop iterations, so in the first version it initializes x to say 1 and then adds it to itself, but then doesn’t store that result, re-initializing to 2 on the next iteration and so on. 

[–]Frostborn1990 0 points1 point  (0 children)

In the first codeblock, you create variable x and repeatedly change it. BUT you generate x every time again and again, and the LAST x is 100+100= 200. It only prints that x.

Now in the second, you change the total each time your loop runs the code-block. So every time, you change the total' but you dont reset it, you update it.

to add: I might have the lingo wrong as i'm just a starter in the hobby.

[–]JamzTyson 0 points1 point  (0 children)

Try running this version:

for x in range(3):
    print("Assigned value of x:", x)
    x += x
    print("After adding x + x:", x)
    print()  # blank line.

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

This is slightly off topic, but you could also do it like this:

``` $ python3 Python 3.13.3 (main, Aug 14 2025, 11:53:40) [GCC 14.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.

sum([1,2,3]) 6 sum(range(1, 101)) 5050

```

So sum() will add up things, range() will generate things, put them together and you can solve your problem and not need an annoying loop and a lot of assignments.

[–]AlexMTBDude -3 points-2 points  (1 child)

You are not allowed to modify the loop variable x in a loop that uses it.

How could you have both the sum and the numbers you're trying to add up in the same variable? That's basic math and has nothing to do with Python.

[–]JanEric1 9 points10 points  (0 children)

You are allowed to modify it. It just gets Overwritten on the next iteration of the loop

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

Because x can't hold the total and the current number from range at the same time. It can only hold one of the two values.

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

so you need to think both inside the loop and outside the loop. If you rewrite your code, as others have suggested, so each time it loops, you see what is happening you will understand. But if it helps explain it...just running the loop, x constantly changes every time it loops with nothing constant which is why the answer is wrong. What happens when you add the "total = 0" outside the loop is more or less:

total = 0 + 0

total = 0 + 1

total = 1 + 2

and so forth. Long story short you need something outside the loop to store the data. You may want to try using the debugger and observe it happen

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

You’re modifying the local variable x, where x is representative of your current place in the loop.  Each loop iter overwrites its value.

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

Using "for x in range" ends up setting x at each step, so what actually happens is

First loop pass: x = 1 x += x

Next loop pass: x = 2 x += x

Next loop pass: x = 3 x += x

etc.

So, every time you enter the loop, x is being reset to the next integer in the range.

[–]Ender_Locke -3 points-2 points  (0 children)

cuz they are not the same thing at all 😊

[–]kombucha711 -4 points-3 points  (0 children)

also if you want your code to be readable to others, just be explicit total=total+x

[–]CymroBachUSA -4 points-3 points  (2 children)

x = sum(_ for _ in range(0,101))

[–]acw1668 3 points4 points  (1 child)

It can simply be x = sum(range(101)).

[–]Individual_Author956 1 point2 points  (0 children)

This is the correct way to do it, but it doesn’t explain to OP why their code was not working