Learn to Code via Tutorials on Repl.it!

← Back to all posts
The Monty Hall Problem! 🐐 🚘
Bookie0 (5653)

Hiya everyone!

Lets start with a dad joke because why not!

Hahahaha— ok whatever…

Yea so recently I was reading a book called The Curious Incident of the Dog in the Night-Time, by Mark Haddon. (Btw its a really great book). In it, they talked about the Monty Hall Problem.

What is it?

Now what is the Monty Hall Problem?

So the Monty Hall Problem is kinda like a brain teaser; its a probability puzzle which was based a bit on a game show Let’s Make a Deal and it was named after the host, Monty Hall.

So imagine in front of you there are 3 doors, and you don’t know what’s behind those doors. 2 doors contains goats and 1 of them contains a car.

The presenter asks you which door you first want to pick. Let’s say you pick Door A. Next, the host, who knows what's behind the doors, opens another door, say Door C, which has a goat. He then says to you, "Do you want to pick door Door B?” Is it to your advantage to switch your choice?

Surprisingly, the odds aren’t 50-50, as you might think, because you have two doors. If you switch doors you’ll win 2/3 of the time!


Aight so its a tad hard to explain, but I will try to explain as EZ and simple as possible ;)

So if you pick a door and stay there, you have a 1/3 chance of winning, because you have a 1 in 3 chance of obtaining the car on the first choice, right?

So that means that the other doors must have the rest of the chances, or 2/3.

Here is a picture to kind of represent it:

What I try to think about it:

So personally, what I think of is this (disclaimer: my explanation might not be that good/valid)

So lets remember that there are more goats than cars, so you have more chances of first picking a goat in the 1st choice.

Lets say you pick Door A, and it has a goat behind it (but you don’t know). Since the presenter will then open a door with a goat next (like door B), then that means that the remaining door must be a car:

Door A: goat (1st choice, user doesn’t know)
Door B: goat (open door, user does know)
Door C: car (user doesn’t know)

In that case, if the user switches, they will win.

So for that specific case that I just mentioned, you have a 2/3 chance of having that case.

Thats why switching is better, because you should assume you have that case where switching will make you win!

Now you might think that its simpler now, but remember that the user doesn’t know what’s behind those doors!


You might think since you have more chances of getting goat in the beginning —and thus having more chances of winning with switching— then that means every time you pick a door it will be a goat. Indeed, you’re most likely to pick a door with a goat behind it, but that doesn’t mean that the possibility of having a door with a car is completely impossible.

See that’s the thing with probabilities. You can never know exactly if the coin you flip will land on heads or tails. Probabilities can help you try to “guess” the future, but you shall never be sure.

Here is a something that a user said, I think it really explains well this problem:

To me, the clearest explanation is that the only way to get it wrong by switching is to have picked the correct door in the first place. The odds of picking the correct door first are 1 in 3. (From a Reddit post)




Myself brain as well! xD

Here are some other sites if you want to know more:




Ok so the repl attached is a simulator based on the point of view of the person playing this challenge. (When prompted, please write exactly or it won’t work properly ;)

pleaseeee don’t look at the code because you will most likely have a stroke or something. What I did could most likely be shortened and made easier….

I hope you enjoyed reading this tutorial, and learnt something new!

As usual, feel free to comment, say your points of views, give feedback, etc. :)

Also I might try to make everything random, meaning that I make the computer do the choices on its own for like 100 times, to show that if you switch you have more chances of winning, so keep an eye out for that! ;)

Anyways, thanks again and Have A Really Fantastic Day!!! :D

aguy11 (125)

Nice tutorial (Yes, your code could be made shorter, but it's good anyway)!

What I thought was really the joke:

Bookie0 (5653)

@aguy11 thanks!

and yes that is a shameful joke lmao lol

DigitCommander (33)

I've read The Curious Incident of the Dog in the Night-Time.

Entia non sunt multiplicanda praeter necessitatem

Which means:

No more things should be presumed to exist than are absolutely necessary.

This is a law called Occam's razor

Bookie0 (5653)

yea, thats a great quote! ;) @DigitCommander

DigitCommander (33)

@Bookie0 Thanks. If you want another bit of proof, Christopher Boone, the main protagonist, does a mental problem called Conway's Soldiers which is when you have an infinite chess board and you can move tiles. BUT you can only move a tile when it can move horizontally or vertically over a coloured tile and when you do this, you have to put a tile where the previous tile was. I don't think you can do it in a repl since it's infinite although there might be a way.
I got the car first try btw.

Bookie0 (5653)

ah nice thanks for that! :D @DigitCommander

amaddentcsec (206)

Seconds?? I thought you were only supposed to eat a minute amount...


BobTheTomatoPie (3194)

please please use .upper()

DynamicSquid (4603)

Yesterday, I ate a clock. It was very time consuming

MatthewXia (25)

im losing brain cells trying to understand

LoveTheBears1O1 (2)

I made my own repl (Python) to prove the Monty hall problem!
Enter the number of times you want it tested (I recommend 100), and it will calculate the number of times the player would switch and win and stay and win.

Bookie0 (5653)

oh wow what a coincidence i am also making the proof for that :) @LoveTheBears1O1

Nettakrim (392)

ah this makes the fatal flaw in assuming i dont want a goat

AustinZhang1 (65)

@Bookie0 I think this is a good picture to represent the problem:

zypherion (0)

Nice, this was one of the very first things I did to help learn python (spoiler, that was only around the beginning of the year).

But my focus was more on "How quickly can we iterate through rounds to prove out the probabilities" and less "Have a user interact in every round and provide feedback"...

So it's interesting to see the two different approaches. Here's mine:

Bookie0 (5653)

yea, i first wanted to do the user interaction because it seemed a tad easier, but im now trying to work on the proof. yours is very good, but mine is going to be probably different and very bad coding lol.

but thanks anyways ;) @zypherion

Bookie0 (5653)

hum yea try again a few times and youre more than likely to get the car! ;) @ironblockhd

k9chelsea2 (765)

hehe I switched lol,

k9chelsea2 (765)

wheres my car?? >:( I don't eve have a license or have an idea how to drive BUT WHERES MY LAMBO???

HahaYes (1900)

Shouldn't this be in the share section?

Bookie0 (5653)

no because this is a tutorial. The repl is just a continuation of the tutorial @HahaYes

HahaYes (1900)

@Bookie0 how does this get 16 upvotes when my essay gets 5 smh

Bookie0 (5653)

dude do you really care about it? also you already asked this @HahaYes

HahaYes (1900)

how does this get 14 upvotes when my essay I wrote about computer science gets 5. Smh repl users

Bookie0 (5653)

do you care about upvotes that much? @HahaYes

BobTheTomatoPie (3194)

omg im proud of you bookie it no longer says upvoting is caring @Bookie0

Bookie0 (5653)

yea im tired of saying it i dont put anything now i guess its better @BobTheTomatoPie

theroadtocode (18)

This is a really interesting brain teaser. I think it was on Brain Games or TedEd riddles... I don't remember which one but I really liked your simulator!

DannyIsCoding (697)

This is very interesting I love these kind of riddles/exercises with probability.

I ate a clock. It was very time consuming

zplusfour (881)

But tell me...

Why you are a good programmer?

Bookie0 (5653)

thanks! ;)

also wdym with your question? @ZDev1

zplusfour (881)

@Bookie0 it is not a question
I just said 'Why are you a good programmer?' just to say that you are too good ;)

SixBeeps (4741)

I'm proud of you, son.
Though you could've just called upper() on the user input instead of asking them to make it uppercase.

Bookie0 (5653)

oh yea, i could do that thanks!

son? lmao

Bookie0 (5653)

hum i tried doing upper(), is it like this?

because when i did it, and typed a (without capitals), it doesnt work... :/

BobTheTomatoPie (3194)

You can also just put upper after the input like input().upper() @Bookie0

LTI2 (115)

@Bookie0 you mean "thanks dad!" lol