The Monty Hall Problem in Python

Posted on

Again, this is another coding exercise in Dream.In.Code. Well, it is actually a challenge but since my submission pales compared to the others, I don’t prefer to call it as one.

What is the Monty Hall Problem?

Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1 [but the door is not opened], and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice?

Basically, you need to prove that switching doors when asked to switch will improve your chances by 2/3 as opposed to the 1/3 chance if you don’t switch doors.

Here’s what I cooked up:

Door Creation

``````def addDoors():
picker =  randint(0,1)
if picker == 1:
door1,door2,door3 = "dragon","goat","goat"
else:
door1 = "goat"
picker = randint(0,1)
if picker == 1:
door2,door3 = "dragon","goat"
else:
door2,door3 = "goat","dragon"
return [door1,door2,door3]``````

Goat Door Opener

``````def openGoatDoor(doors):
door = ""
while door != "goat":
doorNumber = randint(0,2)
door = doors[doorNumber]
return doorNumber``````

The Monty Hall Simulation

``````def montyhall(doorNumber,switchOk):
while goatDoor == doorNumber:
goatDoor = openGoatDoor(doors)
if switchOk == "yes":
doorNumber = [y for y in range(2) if y not in [doorNumber,goatDoor]]
return doors[doorNumber[0]]
else:
return doors[doorNumber]``````

The Tester

``````def testMontyHall(playTimes,switch):
wondragon,wonGoat = 0,0
for i in range(playTimes):
prize = montyhall(randint(0,2),switch)
if prize == "dragon":
wondragon += 1
else:
wonGoat += 1
print "Dragons:",wondragon
print "Goat:",wonGoat``````

The Results
>>> testMontyHall(1000000,”no”)
Dragons: 332961
Goat: 667039
>>> testMontyHall(1000000,”yes”)
Dragons: 666757
Goat: 333243

And this concludes The Monty Hall show. Hope you enjoyed it.