# Win a car¶

### Purpose¶

In this notebook you'll simulate playing a simple game of chance in which you are asked to make two choices. At the end, you'll be asked to submit the outcome of the game. The outcome will be combined with the outcomes of your classmates and analysed in class. Please don't Google the game before you get through the whole notebook and submit your game.

### Running this notebook¶

This is a Jupyter notebook. You should download it and run it through Jupyter on your own machine. The notebook should be run in one session from top to bottom. If you mess up at any stage, you can click Restart & Clear Output from the Kernel menu at the top.

### Background¶

The game we're going to play is adapted from an old TV show. The contestant, after going through a number of rounds, is given the opportunity to win a car. They're presented with three closed doors: A, B and C. They're told that a car has been randomly placed behind one of these doors. The other two doors each have a goat behind them. All the contestant has to do is pick a door to open.

### Starting variables¶

To begin, I'd like you to set each of the following three variables to either True or False in the cell below. Please don't Google anything for now - just use the information currently in your head. Remember, this is just a bit of fun.

• Set einstein to True if you have ever heard of someone called Albert Einstein, False otherwise.
• Set monty to True if you have ever heard of someone called Monty Hall, False otherwise.
• Set savant to True if you have ever heard of someone called Marilyn vos Savant, False otherwise.

So, if you know who Einstein is but not Monty Hall or Marilyn vos Savant then type the following in the cell below and press Shift+Enter.

einstein = True
monty    = False
savant   = False


In [ ]:
# Set these variables as described above.
einstein =
monty    =
savant   =


### Quick check 1¶

Run the cell below to make sure everything at this point is running okay.

In [ ]:
# Assume everything is running okay.
aok = True

# Make sure the einstein variable is set to True or False.
try:
if einstein not in [True, False]:
print("Error: please set the einstein variable to either True or False!")
aok = False
except NameError:
print("Error: please set the einstein variable to either True or False!")
aok = False

# Make sure the monty variable is set to True or False.
try:
if monty not in [True, False]:
print("Error: please set the monty variable to either True or False!")
aok = False
except NameError:
print("Error: please set the monty variable to either True or False!")
aok = False

# Make sure the savant variable is set to True or False.
try:
if savant not in [True, False]:
print("Error: please set the savant variable to either True or False!")
aok = False
except NameError:
print("Error: please set the savant variable to either True or False!")
aok = False

# If there are no problems, print a statement to continue.
if aok:
print("Quick Check 1: everything is running okay, please continue.")


### Beginning the game¶

Below is some Python code to generate a random choice between three doors as pictured above: red, green and blue. The code will randomly choose one of the doors to put the car behind. There's no cheating here - the car is as likely to be behind any one of the doors.

The colour of the door with the car is stored as a string in the variable car. The red door is indicated by the string 'red', the green door by 'green' and the blue door by 'blue'. Please don't look at the value of the car variable, at least until the game is over. If you re-run the cell you will again randomly select a door - possibly the same, possibly different.

In [ ]:
# Python provides a library called random to generate pseudo-random numbers.
import random

# The three doors in a list.
doors = ['red', 'green', 'blue']

# Pick a random door.
car = random.choice(doors)

# Tell the user we've picked.
print("From Python: I've randomly selected a door to put the car behind.")


In the cell above, Python randomly selected a door to put the car behind (implying there are goats behind the other two doors.) You shouldn't have looked at what Python chose - it's stored in the car variable.

Here is your first of two choices as the contestant: pick one of the doors that you think the car might be behind. To indicate your preference, in the cell below set the variable pick to 'red' if you want to pick the red door, 'green' for the green door or 'blue' for the blue.

Here you are being asked to guess what door Python might have chosen, based on no information really - it's just a guess.

In [ ]:
# Pick one of three doors: 'red', 'green' or 'blue'.
pick =


### Quick check 2¶

Run the cell below to make sure everything at this point is running okay.

In [ ]:
# Assume everything is running okay.
aok = True

# Make sure car is set to a door.
try:
if car not in doors:
print("Error: please set the car variable to one of 'red', 'green' or 'blue'!")
aok = False
except NameError:
print("Error: please set the car variable to one of 'red', 'green' or 'blue'!")
aok = False

# Make sure pick is set to a door.
try:
if pick not in doors:
print("Error: please set the pick variable to one of 'red', 'green' or 'blue'!")
aok = False
except NameError:
print("Error: please set the pick variable to one of 'red', 'green' or 'blue'!")
aok = False

# If there are no problems, print a statement to continue.
if aok:
print("Quick Check 2: everything is running okay, please continue.")


### Plot twist¶

The car has now been randomly placed behind one of the doors and you have selected the door you think it might be behind. Next, the host throws in a twist. They open one of the doors that you have not picked and shows you there is a goat behind it.

Note there is definitely a goat behind at least one of those two doors, and possibly behind both if you have already picked the car. The producers know where the car is and they make sure the host opens a door with a goat behind it.

Run the cell below to simulate the host opening one of the doors.

In [ ]:
# Pick a random door that was neither picked by the contestant nor has the car behind it.
show = random.choice([door for door in doors if door != car and door != pick])

# Figure out which door was not opened or picked.
notopen = [door for door in doors if door != pick and door != show][0]

# What the host says.
print("\nHost:")
print(f"""
You chose the {pick} door. The producers have told me I can reveal to you that there is a goat behind the {show} door.
My question to you now is this: do you want to stick with the {pick} door or do you want to switch to the {notopen} door?
""")


So, the host asks if you want to stick with your current door or change to the remaining door that you have not picked and they have not opened. Once you make your decision, all doors will be opened to reveal which one has the car behind it.

In the next cell, indicate whether you will stay with your current choice of door or switch by setting the stay variable to True or False.

• Set stay to True if you would like to stick with your current choice of door.
• Set stay to False if you would like to switch to the other door offered by the host.

In [ ]:
# You want to stay with your current door: True or False?
stay =


### Quick check 3¶

Run the cell below to make sure everything at this point is running okay.

In [ ]:
# Assume everything is running okay.
aok = True

# Make sure the stay variable is set to True or False.
try:
if stay not in [True, False]:
print("Error: please set the stay variable to either True or False!")
aok = False
except NameError:
print("Error: please set the stay variable to either True or False!")
aok = False

# If there are no problems, print a statement to continue.
if aok:
print("Quick Check 3: everything is running okay, please continue.")


### The reveal¶

Run the cell below to see if you would have won the car. Remember it's just a bit of fun so it doesn't matter whether or not you won.

In [ ]:
# Tell the user which door was opened by the host.
print()
print("Host:")
print(f"""
You originally picked the {pick} door and I showed you there was a goat behind the {show} door.
I asked you if you wanted to stay with the {pick} door or change to the {notopen} door.
You decided to {'stay' if stay else 'change'}.
I can now reveal to you that the car was behind the {car} door.
You have {'won' if car == [pick, notopen][not stay] else 'not won'}.
""")


### What to submit to the course page¶

Please run the following Python code and copy and paste the output below it to the link on the Moodle page.

In [ ]:
print("Please copy and paste the following output into the textbox on the Moodle page:")
print()
print(f"{einstein},{monty},{savant},{car},{pick},{stay}")