Skip to content Skip to sidebar Skip to footer

Python Oop Monty Hall Not Giving The Expected Results

Trying out some OOP in python, I tried to create a Monty Hall Problem simulation that is giving odd results. I implement three different strategies that a player can choose from,

Solution 1:

You're not playing the game correctly. After the contestant chooses a door, the host reveals a goat behind one of the other two doors, and then offers the contestant the opportunity to switch -- you're allowing a choice between three doors instead of two. Here's a revised play() function:

defplay(strategy):
    player = Player()
    items = [Goat(), Goat(), Car()]
    doors = [Door(name='a'), Door(name='b'), Door(name='c')]

    random.shuffle(items)

    for door in doors:
        item = items.pop()
        door.behind = item

    player.choose(random.choice(doors))

    # player has chosen a door, now show a goat behind one of the other two

    show = Nonefor door in doors:
        ifnot (door.is_open or door.is_chosen) and door.behind.is_a == 'goat':
            show = door
            show.open()
            break# The player has now been shown a goat behind one of the two doors not chosenif strategy == 'random':
        if random.choice([True, False]):
            for door in doors:
                ifnot (door.is_open or door.is_chosen):
                    final = door
                    breakelse:
            final = player.door

    elif strategy == 'switch':
        for door in doors:
            ifnot (door.is_open or door.is_chosen):
                final = door
                breakelif strategy == 'stay':
        final = player.door

    player.choose(final)

    return player.open()

That produces results like:

results:    strategy=random wins=4977   loses=5023results:    strategy=switch wins=6592   loses=3408results:    strategy=stay   wins=3368   loses=6632

Post a Comment for "Python Oop Monty Hall Not Giving The Expected Results"