Running total within a loop
Running total within a loop
The purpose of the running total algorithm is to add up a
list of values
The running total always starts at zero
The program asks the user to enter a number, and this
number is added to the running total
At the end of the program, the total is displayed
Running total within a loop
Pseudocode
Pseudocode to add 5 numbers to a running total within a
list
Code
Python code to add 5 numbers to a running total within a
list
Unknown number of values
The previous example only works if we know in advance
how many numbers need to be added to the total
In the previous example, the program will always ask the
user for 5 numbers, and add each to the total
But what if we don’t know how many numbers are to be
added?
Pseudocode
Pseudocode to add an unknown number of values to a
running total within a list
Code
Python code to add an unknown number of values to a
running total within a list

SD & D Running Total

  • 1.
  • 2.
    Running total withina loop The purpose of the running total algorithm is to add up a list of values The running total always starts at zero The program asks the user to enter a number, and this number is added to the running total At the end of the program, the total is displayed
  • 3.
  • 4.
    Pseudocode Pseudocode to add5 numbers to a running total within a list
  • 5.
    Code Python code toadd 5 numbers to a running total within a list
  • 6.
    Unknown number ofvalues The previous example only works if we know in advance how many numbers need to be added to the total In the previous example, the program will always ask the user for 5 numbers, and add each to the total But what if we don’t know how many numbers are to be added?
  • 7.
    Pseudocode Pseudocode to addan unknown number of values to a running total within a list
  • 8.
    Code Python code toadd an unknown number of values to a running total within a list