Average Calculator

Code helper image

Step 6 – Calculate the mean, median, and mode

Now we need to calculate the mean, median, and mode.

We could do this by hand, but that would involve a couple of bits of quite tricky code that I don't want to introduce just yet.

Instead, we'll let Python help us and use some built in methods.

  1. At the top, add the code to import a set of methods that deal with statistics

    Statistics is the part of maths that deals with things like averages. The module called statistics includes methods that we can use

  2. Then add the three lines of code at the bottom to calculate the mean, median, and mode

    IMPORTANT: the word statistics is quite tricky to type!

  3. Run your code and check it all works

Test it to make sure that everything works. What happens if there's only one test? Or if there aren't any tests? Or if the marks are all the same?

Checking the mode

Here's curious thing.

The mode is the most frequently occurring value in the set of numbers. So in the set [3, 5, 3, 6, 2, 3, 7, 5] the mode is 3 – there are three 3s, two 5s and one of everything else.

Test what happens when there's only one of each value that gets entered. e.g. [34, 56, 78, 21]. What does Python tell you is the mode? Why do you think that is?

How about with a set like [34, 78, 56, 56, 78, 21]? There are two most frequently occurring values. Which one does Python say is the mode? Why?