# Simulates the Monty Hall problem.
# - A car is hidden behind 3 doors.
# - The contestant randomly choses a door.
# - The game host opens a door behind which there is no car.
# - The contestant then has the option to change her mind and open another door.
# Prompts the user for the number of times the game is played,
# and whether the contestants opts for swiching door or not.
# Displays the details of the game for the few first games,
# set to a maximum of 6 by default,
# and prints out the proportion of games being won.
# Written by Eric Martin for COMP9021

from random import choice, randrange

def set_simulation():
    while True:
            nb_of_experiments = int(input(
                'Enter the number of times the simulation should be run: '))
            if nb_of_experiments <= 0:
                raise ValueException
            print('Your input is incorrect, try again.')
    while True:
        contestant_switches = input(
            'Do you want the contestant to switch door? ')
        if contestant_switches in {'Yes', 'yes', 'Y', 'y'}:
            contestant_switches = True
        if contestant_switches in {'No', 'no', 'N', 'n'}:
            contestant_switches = False
        print('Your input is incorrect, try again.')
    return nb_of_experiments, contestant_switches

def simulate(nb_of_simulations_to_display = 6):
    nb_of_experiments, contestant_switches = set_simulation()

    print('Starting the simulation with the contestant ', end = '')
    if not contestant_switches:
        print('not ', end = '')
    print('switching doors.\n')

    nb_of_wins = 0
    for i in range(nb_of_experiments):
        doors = ['A', 'B', 'C']
        if i < nb_of_simulations_to_display:
            winning_door = choice(doors)
            print('\tContestant does not know it, but car '
                  'happens to be behind door {:}.'.format(winning_door))
        first_chosen_door = doors.pop(randrange(3))
        if i < nb_of_simulations_to_display:
            print('\tContestant chooses door {:}.'.format(first_chosen_door))
        if first_chosen_door == winning_door:
            opened_door = choice(doors)
            if contestant_switches:
                second_chosen_door = doors[0]
                nb_of_wins += 1
            opened_door = doors[0]
            if contestant_switches:
                second_chosen_door = winning_door
                nb_of_wins += 1                
        if i < nb_of_simulations_to_display:
            print('\tGame host opens door {:}.'.format(opened_door))
            if contestant_switches:
                print('\tContestant chooses door {:} '.format(
                                            second_chosen_door), end = '')
                print('\tContestant chooses door {:} '.format(
                                             first_chosen_door), end = '')
            if (first_chosen_door == winning_door) ^ contestant_switches:
                print('and wins.\n')
                print('and looses.\n')            
        elif i == nb_of_simulations_to_display:
    print('Contestant has won {:.2f}% of games.'.format(
                            nb_of_wins / nb_of_experiments * 100))

if __name__ == '__main__':

Resource created Wednesday 05 August 2015, 10:56:41 AM, last modified Wednesday 05 August 2015, 11:04:34 AM.

file: monthy_hall.py

Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G