How does a program add several values?
A program may need to add marks, prices, scores or measurements as each value is entered.
Totalling uses a variable that stores the running total. Each new value is added to the total already stored.
Totalling means adding values to a running total. Each new value is added to what the program has already collected.
A program may need to add marks, prices, scores or measurements as each value is entered.
Totalling uses a variable that stores the running total. Each new value is added to the total already stored.
The statement Total <- Total + Value uses the old value of Total, adds the new value, then stores the result back in Total.
This statement is usually placed inside a loop so that several values can be added.
Imagine a scoreboard. When a team scores, the points are not written on a new empty board.
The new points are added to the score already displayed. The scoreboard then shows the updated total.
The values 4, 7 and 3 are added to a running total. The total is initialised before the loop begins.
Total <- 0
FOR Count <- 1 TO 3
INPUT Number
Total <- Total + Number
NEXT Count
OUTPUT Total
total = 0
for count in range(3):
number = int(input("Enter a number: "))
total = total + number
print(total)
A complete totalling routine usually needs three clear parts: initialise the total, update it inside the loop, then output it after the loop.
Using the exact variable names given in the question also helps keep the algorithm accurate.
If the total is not initialised, the program may try to add a value to an unknown value.