The Monty Hall Problem

In [ ]:
import random
In [ ]:
n_total = 0
n_correct = 0
while True:
    doors = [None, None, None]
    doors[random.randrange(3)] = 1000000
    n_total += 1
    guess = int(input('Enter a guess:'))
    if guess == 0:
        if doors[1] is None:
            other_empty = 1
            switch_to = 2
        else:
            other_empty = 2
            switch_to = 1
    elif guess == 1:
        if doors[0] is None:
            other_empty = 0
            switch_to = 2
        else:
            other_empty = 2
            switch_to = 0
    else:
        if doors[0] is None:
            other_empty = 0
            switch_to = 1
        else:
            other_empty = 1
            switch_to = 0
    print('Door', other_empty, 'is empty.')
    print('Do you want to switch to door', switch_to, '?')
    y_or_n = input('Y/N: ')
    if y_or_n == 'y' or y_or_n == 'Y':
        guess = switch_to
    if doors[guess] is not None:
        print('You guessed right!')
        n_correct += 1
    else:
        print('You guessed wrong!')
    print('Win percentage:', n_correct / n_total * 100)
In [ ]:
n_total = 0
n_correct = 0
for _ in range(100000):
    doors = [None, None, None]
    doors[random.randrange(3)] = 1000000
    n_total += 1
    guess = 0
    if guess == 0:
        if doors[1] is None:
            other_empty = 1
            switch_to = 2
        else:
            other_empty = 2
            switch_to = 1
    elif guess == 1:
        if doors[0] is None:
            other_empty = 0
            switch_to = 2
        else:
            other_empty = 2
            switch_to = 0
    else:
        if doors[0] is None:
            other_empty = 0
            switch_to = 1
        else:
            other_empty = 1
            switch_to = 0
    y_or_n = 'y'
    if y_or_n == 'y' or y_or_n == 'Y':
        guess = switch_to
    if doors[guess] is not None:
        n_correct += 1
print('Win percentage:', n_correct / n_total * 100)
In [ ]:
guess
In [ ]:
def montecarlo_montyhall(n_trials, n_doors, n_to_elim, switch):
    n_total = 0
    n_correct = 0
    for _ in range(n_trials):
        doors = [None, None, None]
        doors[random.randrange(3)] = 1000000
        n_total += 1
        guess = 0
        if guess == 0:
            if doors[1] is None:
                other_empty = 1
                switch_to = 2
            else:
                other_empty = 2
                switch_to = 1
        elif guess == 1:
            if doors[0] is None:
                other_empty = 0
                switch_to = 2
            else:
                other_empty = 2
                switch_to = 0
        else:
            if doors[0] is None:
                other_empty = 0
                switch_to = 1
            else:
                other_empty = 1
                switch_to = 0
        if switch:
            guess = switch_to
        if doors[guess] is not None:
            n_correct += 1
    return (n_correct / n_total) * 100
In [ ]:
montecarlo_montyhall(100, 0, 0, False)
In [ ]:
doors = [None] * 5
doors[1] = 1000000
random.shuffle(doors)
doors
In [ ]:
guess = 1

other_empties = {i for i in range(len(doors)) 
                 if doors[i] is None} - {guess}

n_to_elim = 3
to_elim = []
for i in range(n_to_elim):
    to_elim.append(other_empties.pop())

switch_to = set(range(len(doors))) - {guess} - set(to_elim)

print(switch_to)