# Flipping a coin using Python answers - 5

We have written an algorithm describing an experiment for flipping a coin.

**Q1.** You can copy this code into Python and run it.

**import random**

**headCount = 0****galleyCount = 0**

**print('Welcome\n')****flips=int(input('How many times do you want to flip the coin? >>> '))**

**for cointoss in range(flips):**** flip = random.randrange(1,3)**** if flip == 1:**** headCount = headCount + 1**** if flip == 2:**** galleyCount = galleyCount + 1**

**print('\nTotal number of heads:',headCount)****print('Total number of galleys:',galleyCount)**

**input('\nPress <ENTER> to quit >>> ')**

**Q2.** The program imports the random module.

We then initialise the variables we need.

We print out a welcome message - note the \n which forces the cursor to go to the next line.

We input the number of times to flip the coin, converting it from a text value to an integer and storing it in flips.

We then flip the coin however many times requested.

random.randrange(1,3) produces a random number starting at 1 and up to but not including 3.

If it's a 1, we increase the head counter. If it's a 2, we increase the gallery counter.

We then prin the results and wait until ENTER is pressed, before exiting the program.**Q3 - Q4 **Results should be about 50 - 50. The point here is the amount of time the computer is saving us!**Q5.** You can copy this code into Python and run it.

**import random**

**print('Welcome\n')****flips=int(input('How many times do you want to flip the coin? >>> '))****experiment=int(input('How many times do you want to run the experiment? >>> '))**

**for times in range(experiment):**** headCount = 0**** galleyCount = 0**** for cointoss in range(flips):**** flip = random.randrange(1,3)**** if flip == 1:**** headCount = headCount + 1**** if flip == 2:**** galleyCount = galleyCount + 1**

** print('\nExperiment number',times+1)**** print('Total number of heads:',headCount)**** print('Total number of galleys:',galleyCount)**

**input('\nPress <ENTER> to quit >>> ')**

**Extension work**

You can copy this code for a dice model into Python and run it. The important thing here is to ensure that students write out an algorithm first.

**import random**

**print('Welcome\n')****rolls=int(input('How many times do you want to roll the dice? >>> '))****experiment=int(input('How many times do you want to run the experiment? >>> '))**

**for times in range(experiment):**

** oneCount = 0**** twoCount = 0**** threeCount = 0**** fourCount = 0**** fiveCount = 0**** sixCount = 0**

** for diceroll in range(rolls):**** roll = random.randrange(1,7)**** if roll == 1:**** oneCount = oneCount + 1**** if roll == 2:**** twoCount = twoCount + 1**

**if roll == 3:**

**threeCount = threeCount + 1****if roll == 4:**

**fourCount = fourCount + 1****if roll == 5:**

**fiveCount = fiveCount + 1****if roll == 6:**

**sixCount = sixCount + 1**

**print('\nExperiment number',times+1)**

**print('Total number of ones:',oneCount)**

**print('Total number of twos:',twoCount)**

**print('Total number of threes:',threeCount)**

**print('Total number of fours:',fourCount)**

**print('Total number of fives:',fiveCount)**

**print('Total number of sixs:',sixCount)****input('\nPress <ENTER> to quit >>> ')**

Our model is a reasonably good one. Students could be asked to investigate how 'random' the random numbers being generated by the random module actually are. The output could usefully be represented as a diagram, although that is beyond the scope of this work. It is important that students start to write algorithms describing problems and solutions accurately, and test thoroughly their programs with a range of data by predicting before running the code the expected outcomes. Depending upon where the students are with their Python programming, you could ask some students to modify their program so that it rolls two dice, adds up the score and keeps track of the number of times each total happens. This will produce much more interesting data than a single dice roll.