Back

Flipping a coin using Python - 4

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

Q1. Type the following code into Python and run it without errors.

python1

Q2.  Compare this working program to the algorithm we wrote. With the help of your teacher, describe how this program works line by line.
Q3. Run the program for an experiment with 100000 (one hundred thousand) flips of the coin. Write down the result.
Q4. Repeat the experiment 5 times. Write the results down each time. Would you say that it is heads about half the time and galleys about half the time?

Rather than manually run the experiment lots of times, we could modify our algorithm:

import a random number generator

1. Display a welcome message.
2. Ask the user how many times they want to flip the coin. Store the result in 'flips'
3. Ask the user how many times they want to run the experiment. Store the result in 'experiments'
4.
5. For 'experiments' times, do the following:
6.     Set the heads counter = 0
7.     Set the galley counter = 0
8.      For 'flips' times, do the following:
9.           Pick a random number, either 1 or 2.
10.         If the random number = 1
11.               Increase the head counter.
12.         If the random number = 2
13.               Increase the galley counter.
14.
15.    print ('Experiment number:', times+1)
16.    print ('Total heads:', headCount)
17.    print ('Total galleys:', galleyCount)
18. Display a goodbye message and finish

Q5. Modify your program and run it. Test it with various values.

python2

Extension work
a) Write an algorithm that models the roll of a 6-sided dice being rolled lots of times in an experiment. You should be able to take the algorithm for Q5 above and modify it. Ask the user how many times they want to roll the dice in each experiment, and how many times they want to run the experiment.
b) When your algorithm is written, write a Python program for the dice roll experiment and test it thoroughly. No new Python code over and above what was used in Q5 is required for this question.
c) How realistic is the model of the dice? How could we improve our model?

Back