Sum of numbers

In our program we will ask for a range of numbers, and then prints the sum of the numbers. For example, if you introduce 6 as de lower and 10 as the higher, we will print 40. Because 6+7+8+9+10=40.

captura-de-pantalla-2017-02-27-a-las-11-07-22

First we declarate the low and the high input like int, because they will be integer numbers. Then, we declarate the result like cero to start. We need a counter to stop the loop when we raise the higher number. This is the a and we start like low, because we will start in the lower number that the user introduce. We also declarate new like low, because is the number we will be adding and it also going to star in the lower number. Top will be the number where we stop, but it doesn’t come into the loop, that’s the reason why we sum 1, to avoid stop 1 number before we want.

Inside the loop, all we are doing, is adding, incrementing  the counter a to comparate with the top, and also incrementing new to do the sum.

I think this video could help you to understand better de loops: Python 3 Programming Tutorial – For loop.

Finally, we print the result with a sentence. It also remember us the original numbers we introduced.

Mastery topics: When to use what type of repetition in a program/Basic user input

Leave a comment