Module 3 - Section 1 - Loops
3.1.4 - Calculating a sum
A common operation in working with loops is to keep a running total. This “running total” code pattern is used a lot. Keep a running total of a score, total a person's account transactions, use a total to find an average, etc. In the code below, the user enters five numbers and the code totals up their values. Copy the code below and save it as sum.py inside your working folder.
So how can we calculate this sum 1+2+3+4+5+6+7+8+9+ ...... + 1000? What is the range of these numbers? Is it range(1000) or range (1,1001)? If you don't remember try to find listing the range. Of course feel free to think something different
The commands are
>>> list(range(1000))
and>>> list(range(1,1001))
A solution to the problem is :
Carefully remove the # signs and take care of indentation to see the program running and printing information about what is doing. This a form of debugging.
So this program calculated the sum of even numbers between 0 and 100 (not included). Can you modify it to calculate the odd numbers from 1 to 99? You need to change only one number, but you can find your own solution. The answer is 2500.