OneCompiler

Monty_hall_example.py

305

import random as rn

import random as rn

def result():
doors = ["lambo", "goat", "goat"]
unmodified_doors = doors
# radnomise doors

rn.shuffle(doors)

# randomly select a door

choice = rn.choice(doors)

doors.remove(choice)
# open a door



cont = True
i = 0
door_to_reveal = ""

for z in doors:
    if doors.index(z) == choice:
        i += 1
        continue
    elif z == "lambo":
        i += 1
        continue
    else:
        door_to_reveal = z
doors.remove(door_to_reveal)


outcome = ""

# decide to make our second choice
decide_to_change = bool(rn.getrandbits(1))
if decide_to_change:
    if doors[0] == "lambo":
        outcome = "win"
    else:
        outcome = "loss"
else:
    if doors[0] == "lambo":
        outcome = "win"
    else:
        outcome = "loss"

if outcome == "win":
    return True
else:
    return False

results = []

num_trials = 1000
tracking = 0
while tracking < num_trials:
f = 0
while f < 100:
outcome = result()
if outcome:
results.append("win")
f += 1
else:
results.append("loss")
f += 1
tracking += 1

wins = results.count("win")
mean = wins / num_trials
print("The mean of the percentage of wins is: " + str(mean) + "%")