Skip to content

Monty Hall Algorithm

Three doors. One prize, two goats. You pick a door, the host opens a different one — always a goat — and offers to let you switch. Most people are certain that switching can't matter. Most people are wrong, and by the end of this lab your program will have proved it ten thousand times.


Overview

You'll learn to write an algorithm in natural language — a step-by-step solution in plain English, precise enough for someone else to follow exactly. Then you'll put the skill to work: write an algorithm for simulating the famous Monty Hall problem, and implement it as a Python program that settles the stay-or-switch argument with data.

Builds on: Unit Testing Your Connect4 Library — you described your make_move algorithm in words there; here we make that skill the whole point.

You've got it when…

  • Your natural language algorithm covers sequencing, selection, and iteration.
  • Another student could implement your algorithm without asking you questions.
  • Your program runs at least 10,000 simulated games and counts wins for both staying and switching.
  • Your printed results settle the question: does switching matter?

Collaboration & AI

Work: On your own. Trading algorithms with a neighbor to test whether they can follow yours is encouraged — that's the test that matters.

AI — AIAS Level 1, No AI: Describing a process precisely is the skill this lab builds. Write the words and the code yourself. What the levels mean.


Writing an Algorithm in Natural Language

Before writing code, programmers often describe their solution as a step-by-step set of instructions. This description is called an algorithm.

An algorithm written in natural language uses ordinary English sentences instead of programming syntax. Your goal is to describe the steps clearly enough that someone else could follow them exactly.

A good algorithm description contains three ingredients.

Sequencing

Steps occur in a specific order. Example:

  1. Ask the user to enter a number.
  2. Multiply the number by 2.
  3. Display the result.

If the order changes, the outcome may change.

Selection (Decision Making)

Sometimes the algorithm must choose between different actions depending on a condition. Natural language examples:

  • If the number is greater than 10, display "Large number".
  • Otherwise, display "Small number".

Selection usually uses words like if, otherwise, when, and unless.

Iteration (Repeating Steps)

Sometimes a step must repeat multiple times. Natural language examples:

  • Repeat the following steps 5 times.
  • Continue asking for input until the user enters 0.
  • For each item in the list, print the item.

Iteration describes loops without using programming syntax.


Writing Clear Steps

Each step should:

  • Start with an action verb
  • Describe exactly one action
  • Be clear and unambiguous

Good example:

  1. Ask the user for their age.
  2. Convert the input to a number.
  3. If the age is 18 or greater, display "Adult".
  4. Otherwise display "Minor".

Poor example:

Do something with the age and show the result.

The poor example is unclear and cannot be followed precisely.

Avoid programming syntax

Natural language algorithms should not look like code. Avoid things like:

if age >= 18:
    print("Adult")

Instead write: If the age is greater than or equal to 18, display "Adult".

Be precise

Imagine you are giving instructions to a very literal robot. "Get a number and do math with it" gets you nowhere. "Ask the user to enter a number. Multiply the number by 3. Add 5 to the result. Display the final value." gets you a program.


Example Algorithm (Guessing Game)

Example written in natural language:

  1. Generate a random number between 1 and 10.
  2. Ask the user to guess the number.
  3. If the guess equals the random number, display "Correct!".
  4. Otherwise display "Try again."
  5. Repeat steps 2–4 until the user guesses correctly.
  6. Display the number of guesses the user made.

This algorithm includes sequencing (ordered steps), selection ("If the guess equals…"), and iteration ("Repeat steps…").


Challenge

The Monty Hall Problem is a famous probability puzzle based on a game show scenario. In the game, a contestant chooses one of three doors. Behind one door is a prize, and behind the other two doors are goats.

After the contestant picks a door, the host — who knows where the prize is — opens one of the remaining doors that does not contain the prize. The contestant is then given a choice:

  • Stay with their original door, or
  • Switch to the remaining unopened door.

Many people believe that switching and staying are equally likely to win, but probability theory suggests otherwise.

Your task is to design an algorithm and then implement a simulation to test this claim.

Part 1 — Write an Algorithm (Natural Language)

Before writing any code, write a step-by-step algorithm in natural language describing how to simulate the Monty Hall problem.

Your algorithm should include sequencing, selection (decisions such as which door the host opens), and iteration (repeating the simulation many times).

Your algorithm should clearly describe how to:

  1. Randomly place the prize behind one of three doors.
  2. Simulate the contestant selecting a door.
  3. Determine which door the host opens (a door that is not the prize and not the contestant's choice).
  4. Determine the result if the contestant stays with their original choice.
  5. Determine the result if the contestant switches to the remaining unopened door.
  6. Repeat the experiment many times (for example, 10,000 simulations).
  7. Count how many times each strategy wins.
  8. Display the results.

Your algorithm should be written so that another student could implement it directly.

Checklist before moving on

Ask yourself:

  • Are the steps in the correct order?
  • Is every step clear and specific?
  • Are decisions described using words like if or otherwise?
  • Are repeated actions clearly described?
  • Could another student follow the instructions exactly?

If the answer is yes, your algorithm is ready to be implemented in code.

Part 2 — Implement the Simulation

Write a program that implements your algorithm.

Your program must:

  • Run a large number of trials (at least 10,000 recommended).
  • Track wins when the contestant stays and wins when the contestant switches.
  • Print the results.

Run your program. Your screen should look like this.

Total simulations: 10000
Wins when staying: 3345
Wins when switching: 6655

(Your counts will differ a little — it's random — but the ratio should tell a consistent story.) You may also display percentages if you wish.


Turn It In

  • Your natural language algorithm, written in your notebook.
  • Your program (.py file).

How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent Your natural-language algorithm has all three ingredients — sequencing, selection, iteration — and is precise enough for another student to implement without questions; your program runs at least 10,000 trials, counts wins for both staying and switching, and the printed results settle the argument (roughly one third to two thirds).
3 — Above Average Both the algorithm and the working program are turned in, with a minor gap — one vague step, or output that reports numbers without labels.
2 — Average The algorithm or the program, but not both — or a simulation that runs but only counts one strategy.
1 — Below Average An algorithm that's really just code with the colons removed, or a program that doesn't run.
0 — Failing Nothing in the notebook and no program.