Login

Back

Flipping a coin model - 3

denariusAlthough our coin looks different on each side, and this might change the results of any experiment ever so slightly, we are going to assume in our model that the coin is the same on both sides. Looking at the problem, our first job is to describe what we want our model to do.

1. We want to flip a coin a number of times.
2. If it is 'heads' we want to increase a counter for heads.
3. If it is a 'galley' we want to increase a counter for galleys.
4. We want to print out the results.

This is a good first attempt and summarises what we want to do. Next, we need to try and break down each step into as much detail as possible. We may have to come back and revisit our model later, but a second attempt might go like this:

1. Set the heads counter = 0
2. Set the galley counter = 0
3. Display a welcome message.
4. Ask the user how many times they want to flip the coin. Store the result in 'flips'
5. For 'flips' times, do the following:
6.       flip the coin
7.       If the coin if a head
8.           Increase the head counter.
9.       If the coin is a galley
10.        Increase the galley counter.
11. Display the results
12. Display a goodbye message and finish

When we say 'flip the coin' on line 6, what we will actually do is to generate a random number, either a 1 or 2. If it's a 1, then we will say that that is a head. If it's a 2, then we will say that that is a galley. To generate a random number, we need to import a toolbox of random number generator tools. Let's re-write our description one more time:

import a random number generator

1. Set the heads counter = 0
2. Set the galley counter = 0
3. Display a welcome message.
4. Ask the user how many times they want to flip the coin. Store the result in 'flips'
5. For 'flips' times, do the following:
6.       Pick a random number, either 1 or 2.
7.       If the random number = 1
8.           Increase the head counter.
9.       If the random number = 2
10.        Increase the galley counter.
11. Display the results
12. Display a goodbye message and finish

Now we have a description of flipping the coin that we can work with. This is called an 'algorithm'. This big, fancy word means no more than a description of the solution to a problem. Solutions can be described using diagrams such as flowcharts, but another way is to simply use written sentences / phrases and words, just like we have done here! The resulting description looks very similar to actual programs and so has a special name - 'pseudo-code'. This word means 'code that isn't real code'. It just sort of looks very much like it! So, an algorithm is simply a set of sentences that give a really good description of how to solve a problem, so good, in fact, that it is impossible (in theory) to misunderstand or misinterpret how the solution is constructed.

The advantage of writing algorithms in pseudo-code like we have done above is that any programmer can then program the problem in their own language. For example, look how we used words like 'for ' and 'if' - these are found in many programming languages. Look how indentation is used - very important to help us understand a program and very important in programming languages. Pseudo-code is 'language-independent' - it is written in a way that can then be translated into any actual programming language like Python or Java or BASIC, for example.

You are not born knowing how to write pseudo-code! It is something that you get better with the more you practice but the good news is that there are no rules when you write it! Pseudo-code uses whatever words, phrases and layout you want, although it really helps if you write it in a way that programmers like! Let's see how we can turn our pseudo-code into a program in Python.

Back