Write a program to simulate probability in some way, optionally graphing the results. This challenge started 2021 Jan 12 and ended 2021 Jan 26.
import turtle as trtl
import random
wn = trtl.Screen()
dice = trtl.Turtle()
writer = trtl.Turtle()
grapher = trtl.Turtle()
icon = "icon.gif"
wn.addshape(icon)
dice.shape(icon)
writer.hideturtle()
writer.pencolor('white')
writer.penup()
writer.goto(0, -25)
grapher.hideturtle()
grapher.shape('circle')
grapher.speed(0)
grapher.turtlesize(1)
grapher.pensize(2)
grapher.penup()
grapher.goto(-200, -120)
grapher.pendown()
grapher.goto(-500, -120)
grapher.left(90)
for i in range(20):
grapher.forward(20)
grapher.right(90)
grapher.forward(20)
grapher.backward(20)
grapher.left(90)
x_position = -500
def roll(x,y):
global x_position
writer.clear()
number = random.randint(1,20)
y_position = number * 20 - 120
if x_position < -200:
grapher.goto(x_position, y_position)
grapher.stamp()
x_position += 20
writer.write(str(number), align = 'center', font = ("Helvetica", 50))
dice.onclick(roll)
wn.mainloop()
from random import choice
import turtle
maxtrials = int(input("Number of trials? "))
heads = 0
tails = 0
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
def drawgraph():
scale = int(200 / (heads + tails))
t.clear()
t.penup()
t.goto(-10, 200)
t.write("100%")
t.goto(-10, 150)
t.write("75%")
t.goto(-10, 100)
t.write("50%")
t.goto(-10, 50)
t.write("25%")
t.goto(-10, 0)
t.write("0%")
t.goto(50, -20)
t.write("Heads (" + str(heads) + ")")
t.goto(150, -20)
t.write("Tails (" + str(tails) + ")")
t.goto(0,200)
t.pendown()
t.seth(270)
t.forward(200) # y axis
t.seth(0)
t.forward(200) # x axis
# heads bar
t.penup()
t.goto(50, 0)
t.pendown()
t.seth(90)
t.forward(scale*heads)
t.seth(0)
t.forward(50)
t.seth(270)
t.forward(scale*heads)
# tails bar
t.penup()
t.goto(150, 0)
t.pendown()
t.seth(90)
t.forward(scale*tails)
t.seth(0)
t.forward(50)
t.seth(270)
t.forward(scale*tails)
while heads + tails < maxtrials:
if choice([True, False]):
heads += 1
else:
tails += 1
drawgraph()
turtle.Screen().mainloop()
import random as r
import turtle as trtl
sides = int(input("How many sides?"))
output = r.randint(1, sides)
t = trtl.Turtle()
t.penup()
t.backward(100)
while output > 0:
t.pendown()
t.left(90)
t.forward(50)
t.penup()
t.backward(50)
t.right(90)
t.forward(10)
output = output - 1
wn = trtl.Screen()
wn.mainloop()