+ 5
Can someone explain me how this code works?
sum = 0 x = 10 while x > 0: sum += x x -= 1 print(sum)
23 Answers
+ 2
In loop “while” - x is decreasing by 1 each iteration from 10 to 1
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
When x becomes 0 - loop quits, as x > 0 is False.
Each iteration sum is increasing +x.
So sum = 0 +10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 55
+ 9
Sum =0
Then while loop runs ...with X value 10
So sum =sum+X --->0+10
Then sum =10 now,
After that X become 9 #decremented
So 10+9=19--->sum
Like this it will add ...at last you get 55...as output
+ 4
Sure Nesar Ahmad Yawari
While loop will fail when the X value become 0 until it become 0 it will be true...
Now you got it..
+ 3
Nesar Ahmad Yawari here,
Till the while loop is true it will add the X value with sum and X value get decremented till the while loop fails and finally it prints the whole sum value...
+ 2
Nesar Ahmad Yawari add the X value one by one with sum you get 55 as output...
+ 2
Hi Riya,
I just got introduced to while loop and I just know what while can do but what is the purpose of sum here? What is sum doing here?
I will be glad if you can write me an answer.
+ 2
Hi Riya,
So at first 10 minus 1 aquals to 9 because we have sum+=x and then we will minus 10 by 1 till 1 because we have while x > 0: and then we have:
10
9
8
7
6
5
4
3
2
1
And we sum them, right?
+ 2
Close. 10 minus 1 because x -= 1
Then 10 + 9 because sum += x
Sum will be 55 when finished. 10+9+8+7...1 = 55
+ 2
The code will add 10 to 1. So
10 + 9 + 8 + 7 ... + 1
sum += x
0 += 10
10 += 9
19 += 8
27 += 7
34 += 6
40 += 5
45 += 4
49 += 3
52 += 2
54 += 1
sum = 55
The sum is the initial value (which is 0). And the purpose of it is to add every x into it.
And think of the *while* loop as a limit or when the *while loop* stops. In your example, x > 0, which means from the value of x until 1 (excluding 0).
+ 2
Thanks MorpheusGranger
+ 2
It takes the sum of all integers from 10 to 1 using 1 decrement in a while loop
+ 2
Thanks a lot saikumat chinna.
Actually the Sololearn community is pretty friendly and helpful.
+ 2
the loop is going to iterate from 10 to 1 but since the print function is outside while loop the variable 'sum' will be equal to 10+9+8+7+6+5+4+3+2+1, that means the print function will not be looped, so you will have only one output of the sum which is equal to 55
+ 2
Thanks Daudasaeed,
And good morning Tahirssk Tahirssk.
+ 1
Hi Riya,
Ok so till the while loop is True it will add 10 which is x value with 0 which is sum and 10 which is x gets decremented till the while loop fails? like how it fails?
I would be glad if you explain a bit more.
Thanks
+ 1
I see, so thats how it works.
Thanks a lot Riya
+ 1
Sorry for bothering Riya,
But the result is 55 after I hit debug, can you please explain how it equals to 55?
I will appreciate an answer.
+ 1
Thanks PythonER.
+ 1
Engineer X Simple explanation...Thanks
+ 1
Loops are used to repeatedly execute a block of program statements. The basic loop structure in Python is while loop. Here is the syntax.