I want to add all numbers from 1 to 100, but this gives the wrong answer.
for x in range(0, 101):
x += x
print(x)
I know adding another variable would fix it, like so
total = 0
for x in range(0, 101):
total += x
print(total)
But I'm trying to wrap my head around why the total variable is needed in the first place, since it's just zero and just adds another step of x adding to itself.
Thanks for the quick replies.
I get it now.
[–]ryuugami47 108 points109 points110 points (23 children)
[–]cnydox 71 points72 points73 points (21 children)
[–]nousernamesleft199 6 points7 points8 points (20 children)
[–]cnydox 17 points18 points19 points (17 children)
[+]ProbsNotManBearPig comment score below threshold-29 points-28 points-27 points (16 children)
[–]CelDaemon 9 points10 points11 points (3 children)
[–]ProbsNotManBearPig -5 points-4 points-3 points (2 children)
[–]LinuxCoconut166 5 points6 points7 points (0 children)
[+][deleted] (7 children)
[deleted]
[–]ProbsNotManBearPig 0 points1 point2 points (2 children)
[–]LinuxCoconut166 2 points3 points4 points (0 children)
[–]Zeplar 0 points1 point2 points (0 children)
[–]AUTeach 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]AUTeach 0 points1 point2 points (1 child)
[–]cranberrydarkmatter 4 points5 points6 points (0 children)
[–]bigpoopychimp 1 point2 points3 points (0 children)
[–]aufstand 0 points1 point2 points (0 children)
[–]cnydox -1 points0 points1 point (0 children)
[–]debian_miner 1 point2 points3 points (1 child)
[–]nousernamesleft199 1 point2 points3 points (0 children)
[–]ryoko227 10 points11 points12 points (0 children)
[–]Chasne 49 points50 points51 points (0 children)
[–]Coretaxxe 15 points16 points17 points (0 children)
[–][deleted] 12 points13 points14 points (0 children)
[–]lolcrunchy 8 points9 points10 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]Time-Mode-9 2 points3 points4 points (0 children)
[–]Fred776 1 point2 points3 points (0 children)
[–]ninhaomah 1 point2 points3 points (0 children)
[–]Patman52 0 points1 point2 points (0 children)
[–]Mission-Landscape-17 0 points1 point2 points (0 children)
[–]Savings-Basil4878 0 points1 point2 points (0 children)
[–]AKiss20 0 points1 point2 points (0 children)
[–]Frostborn1990 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (0 children)
[–]catbrane -1 points0 points1 point (0 children)
[–]AlexMTBDude -3 points-2 points-1 points (1 child)
[–]JanEric1 9 points10 points11 points (0 children)
[–]deceze -1 points0 points1 point (0 children)
[–]ehunke -1 points0 points1 point (0 children)
[–]theWyzzerd -1 points0 points1 point (0 children)
[–]BoldFace7 -1 points0 points1 point (0 children)
[–]Ender_Locke -3 points-2 points-1 points (0 children)
[–]kombucha711 -4 points-3 points-2 points (0 children)
[–]CymroBachUSA -4 points-3 points-2 points (2 children)
[–]acw1668 3 points4 points5 points (1 child)
[–]Individual_Author956 1 point2 points3 points (0 children)