Monty Hall Problem Solution Using Python

Monty Hall Problem Solution Using Python

3 min read


Problem Statement

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, 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? (Whitaker, 1990, as quoted by Vos Savant 1990a)

Standard Assumption:-

  1. The host must always open a door that was not picked by the contestant (Mueser and Granberg 1999).
  2. The host must always open a door to reveal a goat and never the car.
  3. The host must always offer the chance to switch between the originally chosen door and the remaining closed door.

Note:-Try to solve yourself first before heading towards the solution.

Monty Hall Problem


In standard assumptions the probability of winning a car is 2/3 by switching.It can be seen by the behavior of the host.Let’s analyze
From below it can be seen that if a person switches his/her choice he/she will be more likely to win.

General Confusion

Although it can be proved using conditional probability and Baye’s theorem many people still think that switching doesn’t matter. Both unopened doors will have an equal probability.
To break this assumption let’s prove it by Python Program.

Implementation of Problem using Python

#Monty Hall Problem
#Various comments are used to improve readability of code
import random#To choose and guess the probability of winning.
doors=["GOAT"]*3#Initializing each door with door
switch_win=0#No. of times player wins by switching
stick_win=0#No. of times player wins by sticking to initial choice
while j<100000:
    x=random.randint(0,2) #xth door comprise of the prize
    doors=["GOAT"]*3 #switching xth door with car
    for i in range(3):
        if doors[i]!="CAR":
            goat_door.append(i) #Appending all goat_door index to list
    while host_door==ch:
    swap=random.randint(0,1) #To know whether player swaps or not
        if doors[ch]=="GOAT":
        if doors[ch]!="GOAT":


Note:-In above image, it can be seen that the no. of chances of win almost doubles up by swapping. No. of iterations can also be increased to get a more accurate result.

Choose your Reaction!
Leave a Comment