← Back to all posts
##### I do not understand how to
FallingNever

There is a problem: Sum of Consecutive Numbers (Python)

No one likes homework, but your math teacher has given you an assignment to find the sum of the first N numbers.

Let’s save some time by creating a program to do the calculation for you!

Take a number N as input and output the sum of all numbers from 1 to N (including N).

Sample Input
100

Sample Output
5050

Explanation: The sum of all numbers from 1 to 100 is equal to 5050.

I wrote some code, but it did not work. After some research I found this code(it works):
N = int(input())
count=0
summary=0
for i in range(1,N+1):
count+=1
summary+=count

print(summary)

Can someone explain me this? (This is a problem from Sololearn PythonForBeginners>Lists>CodeProject36)

Answered by IMayBeMe [earned 5 cycles]
Voters
FallingNever
hotnewtop
IMayBeMe

Lets go through this code step by step:

`.css-o0gntx{position:absolute;left:-9999px;}.css-1v1ju80{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:relative;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}N = int(input())`

The code above prompts the user to input an integer which in this case will set the upper bound for all the numbers being added.

```.css-o0gntx{position:absolute;left:-9999px;}.css-1v1ju80{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:relative;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}count = 0
summary = 0```

Now, this doesn't do much apart from setting up some variables which will be used later on. Then comes the for loop like this:

```.css-o0gntx{position:absolute;left:-9999px;}.css-1v1ju80{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:relative;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}for i in range(1, N+1):
count += 1
summary += count```

This is the main driving force of the code. What it does is it runs the code N times and each time it makes count equal something one higher like this: 1, 2, 3, and so on. Then it adds count to the final sum and continues the loop. I just want to say this code isn't really the best and there are much better ways to do it like this:

```.css-o0gntx{position:absolute;left:-9999px;}.css-1v1ju80{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;position:relative;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}N = int(input())
sum = 0
while N > 0:
sum += N
N -= 1
print(sum)```

The code above is much more self-explanatory and is overall just a lot cleaner.

CosmicBear

It asks for a number, and stores it in the variable `N`. Then for each number up to N, it adds 1 to the variable `count`. Then it prints out `count`.