Monty Hall and the Python #1: Setting up the Monty Hall Problem

Published June 21, 2021, 4:30 p.m.

In this tutorial we are exploring the so-called "Monty Hall" problem. 

For the uninitiated the problem goes like this:  Monty Hall has you on his show called "Let's Make a Deal".  You are presented with three doors. 

One door has a prize behind it.  Two doors have a goat or a "whammy" or some such thing behind it that you don't want. 

You are invited to choose a door.

Now the interesting thing happens.  Monty Hall then opens one of unpicked doors to reveal a "whammy" (not the prize). 

Monty then offers you the chance to pick the other door or keep your original choice.  Do you pick or not?  What are your chances?  Are they just 50/50 at this point?  Is you chance still 33% even though one of the options are gone?  Have your chances gone up? 

The answer to this riddle is that your chances of getting the prize are 67% if you switch your answer.  I intend to prove this point with some math and some pictures but also with a simple python simulation that we can build together.

I had heard of this problem some time ago, however it was reintroduced to me recently by my mother --herself a mathemetician.  When she told me the answer I didn't believe it at firsrt.  I thought that no matter what the initial state of affairs the final choice must be 50/50.  After all, the prize is guaranteed to be behind one of two remaining doors!  Right?  So keep your door, switch your answer... it must all be a wash. 

My mathemetician mother was so tickled by the answer that she did not well remember the expaination of the answer when she shared it with me.  Being a skeptical sort of person myself, I was inclined to doubt her answer.  After all, I have seen plenty of half-baked or misunderstood scientific ideas that were popularized through media reports that bore very little resemblance to the thing that they professed to be about. 

Naturally, being inclined to solve problems with python and being well-versed in simulation of statistics I imediately conceived of some structure of a python program that would settle the matter for me.  In the process of constructing the simulation I came to understand what the correct answer is, and more importantly, why that answer is correct.  From there it was a relatively simple matter to extend the problem to a problem with more than 3 doors -- say 4 or 5 or 100 or 1000.  And then to have Monty Hall perhaps reveal more than 1 door-- maybe 2 or 3 or 98 or 998.

Finally, after solving the actual problem, I also figured out how to "cheat" the problem.  That is, I can construct a "Monty Hall" style problem where the final choice between two doors is actually 50/50.  Why that solution is a "cheat" will be made clear.

Let's get started!  As always (or at least as most often times)  we will start by importing sys and os.

#Monty Hall Problem Code
import sys, os

I find that I need to use one or both of these mods so I like to have them at the top.

Next we will establish our three choices.

choices = ['A', 'B', 'C']

Now we need to establish one of our choices as the "correct choice".  Let's go back up to the top and include another import:

from random import sample

Now we go back down below our choices and define a correct_choice.  In the process of defining a correct choice we establish what are "wrong choices" are so we might define that in our code as well.

correct_choice = random.sample(choices,1)[0]

wrong_choices = [c for c in choices if c != correct_choice]

 

Now we as the contestant must make our choice.  Here at the beginning I will make this an interactive code.  We will use the 'input' command to promt the user to choose one of the choices.

my_choice = input(f'Choose one of the following {choices}. ')

Here we have used an f-string to allow us to display the available choices.

Ok.  Here is a good time to save the file and do a test run.  I saved mine as 'MH_TUTORIAL.py'.  Go to the terminal window and make sure you are in the same directory that your program is in.  Then run the program:

>>C:\PythonWork>python MH_TUTORIAL.py

Make sure that you haven't made any errors so far.  Speaking of errors, we can add a line here to our presently sucessful code to make sure that the user does not try to make a choice that is not in the list of choices.

if my_choice not in choices:
    sys.exit("You messed up moron!")

See! I told you that we would use the sys module.  This will kindly exit the program without nasty error messages and gently insult the user hoping that they make better choices in the future.

Finally we come to the point where Monty Hall is going to make his revelation.   Here we will make a list of the possible choices that Monty Hall can reveal.  Remeber, he can't reveal what you've already chosen and he can't reveal the correct answer.  Therefore, we can make a list with all of those items as follows:

reveals = [c for c in wrong_choices if c != my_choice]

If we chose the prize then this list will have both of the other choices in it.  If we chose the wrong door then this will only have one choice in it.  Either way we may now 'sample' from this list to establish what will be revealed.

reveal = sample(reveals, 1)[0]
print(f"Monty Hall reveals {reveal}!")

 

In the next tutorial we will figure out how to switch our answer and how to track whether or not we guess correctly.  Thanks for watching!  See you in the next tutorial.

Code so far:

#Monty Hall Problem
import sys, os
from random import sample

choices = ['A', 'B', 'C']

correct_choice = sample(choices,1)[0]

wrong_choices = [c for c in choices if c != correct_choice]

my_choice = input(f'Select one of the following {choices}. ')

if my_choice not in choices:
    sys.exit("You messed up moron!")

reveals = [c for c in wrong_choices if c != my_choice]

reveal = sample(reveals, 1)[0]
print(f"Monty Hall reveals {reveal}!")

 

skip_nextMonty Hall and the Python #2: Testing out our simulation
  • Monty Hall and the Python #1: Setting up the Monty Hall Problem
    (currently viewing)
  • Monty Hall and the Python #2: Testing out our simulation

  • Monty Hall and the Python #3: Collecting results from several simulations

  • Monty Hall and the Python #4: Automating responses and running 10,000 times

  • Monty Hall and the Python #5: Expand and Understand; Monty Hall with 100 doors!

  • Monty Hall and the Python #6: Name those doors OR Generate any number of unique strings.