Project 7: Making Decisions

In project 7 you will implement a version of the game show, “Let's Make a Deal.” Your version of the game show is famous because it demonstrates the Monty Hall paradox.

Exercises

Turn in exercises 27, 28, 29, 30 and 31 with this project.

The Monty Hall Problem

The game show starts with three doors. Behind one of them is a car. Behind the other two are goats. Monty asks you to choose a door. For argument's sake let's say you choose door #1. Then Monty opens a different door to reveal a goat.

Monty then asks you, “would you like to keep the door you chose or switch doors?” The problem is a paradox because most people (even some with a Ph.D.) think that the odds of guessing right are 50/50. They're not, you have better odds if you switch.

Getting Random Numbers

There are two kinds of random numbers on a computer. Random numbers that are good for things like games and random numbers that are good for cryptography. Cryptographic random numbers are so random it's very difficult to guess them, but they take time to generate. The random package has random numbers that are good for games.

Here's how to get a random number between 1 and 3 (including 1 and 3):

import random 
my_number = random.randrange(1, 4) 

Use this code to generate the number of the door with a car behind it. You should assume that the other doors have the goats.

Game Procedure

Your game starts by picking a random number (the number of the door with the car). Then prompt the user for the first guess.

Pick a door:

You program has to determine what Monty's guess will be. There are rules that govern Monty's guess:

  • Monty will always pick a goat.
    • If the contestant picked a door with a car behind it Monty may open any other door.
    • If the contestant picked a door with a goat behind it Monty will open the door with the other goat.

You will need nested if/elif/else statements to determine Monty's pick. Once Monty decides make Monty's choice known:

Monty opens door #3

Prompt the user if they wish to stay or switch.

Do you wish to stay or switch: 

The user must type “stay” or “switch” you don't have to worry about incorrect input. After the second choice reveal whether the user won the car or won the goat.

You won the car!

Test your game thoroughly. While you're developing the game it's a good idea to print out the door that the car is behind so you can verify that it works.

Required Functions

Your program is required to have the following functions:

  1. pick_random_door
    1. No arguments
    2. Returns:
      1. BOOLEAN door1 - True if the car is behind door 1 otherwise False.
      2. BOOLEAN door2 - True if the car is behind door 2 otherwise False.
      3. BOOLEAN door3 - True if the car is behind door 3 otherwise False.
        1. Note: Only one of the return values should be true!
  2. montys_choice
    1. Arguments:
      1. BOOLEAN True if the car is behind door 1
      2. BOOLEAN True if the car is behind door 2
      3. BOOLEAN True if the car is behind door 3
      4. INTEGER The number of the door that was guessed by the contestant
    2. Returns:
      1. INTEGER Monty's choice of a door
  3. has_won
    1. Arguments:
      1. BOOLEAN True if the car is behind door 1
      2. BOOLEAN True if the car is behind door 2
      3. BOOLEAN True if the car is behind door 3
      4. INTEGER The number of the door that was guessed by the contestant
      5. BOOLEAN True if they contestant wants to switch doors, False if they want to stay.
    2. Returns
      1. True if the contestant won the car, False if they won a goat.

Each function should have a docstring describing what it does.

Requirements

  1. You must have a docstring describing your program.
  2. You must have the required functions
    1. The functions must have the exact names as above
    2. The functions must take the exact arguments in the order above
    3. The functions must have docstrings.
  3. The program must say “car” if you win and “goat” if you lose.
  4. All code should be in a function
    1. Put the game input and print functions in main()
  5. Your program must be submitted in a file called project7.py

Starter Code

Sart with the following code. It shows the outline of game play:

import random 
 
def main() : 
    door1, door2, door3 = pick_random_door()
 
    guess = int(input('Pick a door: '))
    monty = montys_choice(door1, door2, door3, guess)
 
    print ('Monty opens door #{}'.format(monty))
    choice = input ('Do you switch or stay: ')
 
    if choice == 'switch' :
        switch = True
    else:
        switch = False
 
    if has_won(door1, door2, door3, guess, switch) : 
        print ('You win the car!')
    else:
        print ('You win a goat.')
 
if __name__ == '__main__' :
    main() 

Advanced

The downloadable code for this week contains a stub for playing the Let's Make a Deal game using the web. Add the required functions from above and complete the game play. Note that the templates contain if statements.

Requirements for the advanced version:

  1. You must have a docstring describing your program
  2. You must have the required functions from the assignment
    1. The functions must have the exact names
    2. They must take the same arguments and return the same values
  3. Your program must be submitted in a file called project7_adv.py
  4. You must have the same directory layout as the download
    1. templates
      1. Your HTML templates go here (you can name them anything you like)
    2. resources
      1. The images must go here (you can rename them or change them if you like)

Grading

  • 5 points for the exercises
  • 5 points for a correct submission
  • 10 points for the correctness of your program.