Running Totals

Running Totals are used to add a list of numbers.

General Algorithm

set total to zero
for each value in list
    add value to the total
next value

Adding numbers as they are entered (Nat4)

#
totalAge = 0

for pupil in range(5):

    age = int(input('Enter age:'))
    totalAge = totalAge + age

print(f'Total age = {totalAge}')

averageAge = totalAge / 5
print(f'Average = {averageAge}')
#

Adding numbers in an array (Nat5)

#
scores = [4,7,9,4,8,2,7]

total = 0

for player in range(len(scores)): # or range(7):

    total += scores[player]
 
#

Adding numbers in an array of records (Higher)

Traversing array by index:

#
total = 0

for student in range(len(students)):

    total += students[student].mark
#

Traversing array by values:

#
total = 0

for student in students:

    total += student.mark
#