+ 3
How does this Python code work?
n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum+i i = i+1 # update counter print("The sum is", sum)
3 Answers
+ 6
There are 10 iterations in total from i=1 till i=n, where n is 10. Each time you add the value of i to the variable sum.
So it is 1+2+3+...+10 which is 55.
+ 7
Avinesh, good explanation.
3.14159265358, I recommend you to try your code in the sandbox with changed parameters like n = 100.
+ 1
yes



