Skip to main content

monty

 #when monty picks, player changes doors
        if monty == (range(3)):
            player = random.choice ( [i for i in door if i not in [player, monty]] )

        #if player picked prize, adds 1 to trueA counter
        if player == prize:
            trueA = trueA + 1

    #creates montynever function
    for i in range ( n ):
        #makes list of doors
        doors = [ i for i in range ( 3 ) ]

        #prize recieves random door
        prize = random.choice ( doors )

        #player picks random door
        player = random.choice ( doors )

        #monty picks last door
        monty = random.choice ( [i for i in doors if i not in[prize, player]] )

        #if player picked prize, adds 1 to trueB counter
        if player == prize:
            trueB = trueB + 1


#print statements for info
print ("Out of " +str(n)+ " games:")
print ("Always switching wins: " +"{0: .7f}" .format(trueA/n)+ "(" +str(trueA)+ " games)")
print ("Never switching wins: " +"{0: .7f}" .format(trueB/n)+ "(" +str(trueB)+ " games)")





wn = turtle.Screen()            #creates window
wn.bgcolor("white")             #makes background color
wn.title("monty3")              #titles the window
poly = turtle.Turtle()          #creates poly
poly.shape("turtle")            #cursor used to draw poly
poly.speed(3)                   #speed at which poly is drawn
poly.color("black")              #color of poly
poly.pensize(1)                 #thic' ness of poly
poly.penup()



poly.goto(-110, -50)
poly.pendown()
poly.forward(250)
poly.penup()
poly.goto (-90 , -50)
poly.pendown()
poly.left(90)
poly.forward((trueA/n)*500)
poly.right(90)
poly.forward(80)
poly.right(90)
poly.forward ((trueA/n)*500)
poly.left(90)


poly.forward(50)
poly.left(90)
poly.forward((trueB/n)*500)
poly.right(90)
poly.forward(80)
poly.right(90)
poly.forward((trueB/n)*500)

poly.penup()
poly.goto(-50,(((trueA/n)*500))-50)
poly.pendown()
poly.write ("{0: .7f}" .format(trueA/n) , False, "center", ("Arial", 10, "normal"))

poly.penup()
poly.goto (80, (((trueB/n)*500))-50)
poly.pendown()
poly.write ("{0: .7f}" .format(trueB/n) , False, "center", ("Arial", 10, "normal"))

poly.penup()
poly.goto(-50, -66)
poly.pendown()
poly.write("Always", False, "center", ("Arial", 10, "normal"))

poly.penup()
poly.goto(80, -66)
poly.pendown()
poly.write("Never", False, "center", ("Arial", 10, "normal"))

poly.penup()
poly.goto(15, -80)
poly.pendown()
poly.write ("The Monty Hall Problem: 100000 games", False, "center", ("Arial", 10, "normal"))

poly.penup()
poly.goto(15, -90)
poly.pendown()
poly.write ("Percentage of games won if you switch always or never", False, "center", ("Arial", 10, "normal"))


poly.hideturtle()
wn.mainloop()