Average Calculator

Step 5 – More than one average

Of course, ther'es more than one way to calculate the average of a set of numbers.

We've calculated the mean. Usually that's what we mean when we say "average".

The other ways to calculate the average of a set of numbers are:

  1. the median – the middle value of the set
  2. the mode – the most frequently occurring value in the set

To calculate either of these we'll need to use a different way of storing the values that get entered.

Storing a set of numbers

To calculate the median or the mode, we need to store the numbers as a set. That means we need to store all of the numbers are separate items.

We could do that by using more than variable, but Python has a better way to do this.

To store more than one value at a time, we can use an idea called an array.

Arrays are a bit like variables, but rather than just being able to store one value, we can store many different values inside the array.

THEORY: An array is a type of data structure. It has a name, just like a variable, but can be used to store more than one value. In Python, arrays are called lists because they're lists of things

First we need to change how we get the data from the user.

Code helper image
  1. Delete the line that sets Total to 0. We won't need this now.
  2. Add a line of code to set up an empty array called testScores

    Note, we're using square brackets here. Arrays use them in Python

  3. Delete the line where we update total by adding the new value to it
  4. Now each time we enter a number we'll add it to the array using a command called append
  5. Then, so that we can see what we've done, we'll print the array out
  6. And we'll need to remove the code that calculates the mean for now
  7. Run your code and check it all works

Test it to make sure that everything works. At this point it won't do anything very much, but you need to check that there aren't any errors in your code.