Activity 2: Writing a program

The goal is to design a Python program that executes one instance of the Monty Hall game. The program gets two inputs from the user: the initial door chosen (an integer between 1 and 3) and the player’s decision on whether to switch doors (Y or N).

The program will be reused in later units (in loops as well as simulation and modeling). At this point, we want students to (i) decide what variables to use and what they are for and (ii) use sequences and nested conditionals to implement one instance of the game.

Before starting to program, discuss what random numbers need to be generated when simulating one game and what variables are needed. The bullets below can guide the discussion in class:

  • There are three doors and we refer to them as doors 1, 2, and 3, with door 1 being the leftmost door.
  • To encode what is behind each door, we only need to know which door contains the car. We use a variable winner to identify the winning door; i.e., winner = 2 means the car is behind door 2.
  • We need to create the initial setup. To do so, place the car behind a randomly chosen door.  This is done by generating a random integer between 1 and 3.
  • The game is ready to begin! The player inputs their initial choice. It needs to be 1, 2, or 3. Store the player’s input in a variable, say player_choice.
  • The next task is to open a door with a goat. We know one of the two  doors not chosen contains a goat. But we can’t just open a random one of the two (one may contain a car).  The code to determine which door to open makes up most of the lines in the program. Once a student understands that task, it becomes a sequence of conditionals. Writing the code without understanding what variables to use can lead to confusion and incorrect code.
    • If the player’s original door choice contains a goat, we choose the remaining door with a goat.
    • If the player’s original door choice contains the car, we choose one of the two doors with a goat at random. It is a good idea to use two variables to record the doors with a goat (goat_door1 and goat_door2). You will also need another variable to contain the randomly chosen integer. For example, luck.
    • Use a variable to record the door to open (open_door) and a variable to record the third door (switch_door).
    • Door open_door is opened (and it has a goat).
    • Next, we need to ask the player if he/she wants to switch. The player provides the decision and we store it. For example, in a variable called want_switch (having values Y/N).
    • If the player decides to switch doors, we do not open door player_choice, but door switch_door.
  • In summary, the variables we use are: winner, player_choice, open_door, and switch_door, as well as goat_door1goat_door2, and luck, which are used when the car is behind door player_choice.

Give students a skeleton of the program and ask them to finish the program. This is suited for a team activity (2-3 students).

  • The code consists of multiple conditional statements, with some being nested.
  • The most complex part of the code is deciding which door Monty Hall should open after the original choice has been made.

View the skeleton and sample code

When students know the data structure lists, they can use a list to simplify the code. See example here.