Learn how to Simulate the monty hall problem in Python
Tags: codenewbie
Published: 2020-11-22 19:33:41

*The [original article](https://kalebujordan.dev/simulate-monty-hall-problem-using-python/) can be found on [kalebujordan.dev](https://kalebujordan.dev/simulate-monty-hall-problem-using-python/)*

Hello Pythonistas,

Today I will give you a brief guide to applying Python to Mathematics World, on this tutorial you're going to simulate the **Monty Hall problem** in Python.

You might be wondering what the hell is this Monty Hall Problem?

**The Monty Hall problem** is a brain teaser, in the form of a [probability](https://en.wikipedia.org/wiki/Probability) puzzle, loosely based on the American television game.

![](https://i1.wp.com/kalebujordan.com/wp-content/uploads/2020/06/monty-hall-problem.png?fit=1024%2C569&ssl=1)

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** 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?.

Well, It's upon your choice and luck to switch or stay with Door No 1 or switch to number 2, Mathematically the person who switches has **twice** the chance of winning a Car.

![](https://kalebujordan.com/wp-content/uploads/2020/06/352px-Monty_closed_doors.svg.png)

The concept is based on probability concentration. Initially when you choose door No 1. There is a **1/3** probability that the car is in that door and **2/3** in the other two.

But when the host another door on those two remaining and tell there is a goat, then the probability that there is car concentrate to the door No 2.

So Mathematically Switching would be a better idea.

Now we have already learned the concepts, It's time to simulate our idea in Python.

We are going to use the Python random Library to generate random choices and open doors containing goat and a simple [tkinter](https://docs.python.org/3/library/tkinter.html) widget to display the overall count.

- app.py

```python
import random 
from tkinter import StringVar , Label, Tk, Entry

window = Tk()
window.geometry('400x100')
window.title('Monty hall simulation')
window.resizable(0, 0)

same_choice = StringVar()
switched_choice = StringVar()
same_choice.set(0)
switched_choice.set(0)
no_sample = Entry()
Label(text='Same choice').place(x=80,y=8)
Label(text ='Switched choice').place(x=80,y=40)
Label(textvariable = same_choice, font =(15)).pack()
Label(textvariable = switched_choice, font=(15)).pack()
no_sample.pack()


def simulate(event):
    same_choice_result  = 0
    switched_choice_result = 0
    samples = int(no_sample.get())
    doors = ['gold', 'goat', 'bed']
    for _ in range(samples):
        simulated_doors = doors.copy()
        random.shuffle(simulated_doors)
        first_choice = random.choice(simulated_doors)
        simulated_doors.remove(first_choice)
        opened_door = simulated_doors[0] if simulated_doors[0]!='gold' else simulated_doors[1]
        simulated_doors.remove(opened_door)
        switched_second_choice = simulated_doors[0]
        if first_choice == 'gold':
            same_choice_result+=1
            same_choice.set(same_choice_result)
        elif switched_second_choice == 'gold':
            switched_choice_result+=1
            switched_choice.set(switched_choice_result)
        else:
            print('That\'s will never happed')

no_sample.bind('<Return>', simulate)
window.mainloop()

```
​

### Output 

Enter the number of Simulation to be computed by the computer and then press Enter to run the Simulation

![](https://kalebujordan.com/wp-content/uploads/2020/06/DeepinScreenshot_select-area_20200623120319.png)

As you can see above Switched choice got as twice as chance compared to the one who stayed with the same choice.

Remember choice made in this Simulation is entirely random but still If you gonna try running the Simulation again, you probably gonna get resembling numbers perhaps not exact.

Hope you find this post interesting, don't forget to subscribe to get more tutorials and tips like this. To get the Full code check out on [My Github](https://github.com/Kalebu/Monty-Hall-Problem-Simulation-in-Python/)

I also recommend you read these;
- [How to track phone number in python](https://kalebujordan.dev/how-to-track-phone-number-in-python/)
-   [How to make a music player in Python](https://kalebujordan.dev/make-your-own-music-player-in-python/)
-   [How to make a digital clock in Python](https://kalebujordan.dev/how-to-make-a-digital-clock-in-python/)
-   [How to make python GUI calculator](https://kalebujordan.dev/how-to-make-a-calculator-with-graphical-user-interface-gui-in-python/)
-   [Learn how to convert image to sound in Python](https://kalebujordan.dev/convert-an-image-text-sound/)


In case of any suggestion or comment, drop it in the comment box and I will reply to you immediately.