import random
import tqdm
import time
import turtle
def move(x, y):
turtle.hideturtle()
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.showturtle()
x = eval(input("请你输入要模拟的实验次数(次数越高,概率越精细):"))
print("正在模拟运算!!!")
for i in tqdm.tqdm(range(1, 500)):
time.sleep(0.01)
try:
q, q1 = [], []
a = 1
b, c = 0, 0
while a <= x:
ran = random.sample(["车", "羊", "羊"], 1)
a += 1
if ran == ["车"]:
b += 1
else:
c += 1
gl = b/a
gl2 = c/a
print("不换选中车的概率是{:-^20.3f},选中羊的概率是{:-^20.3f}".format(gl, gl2))
b1, c1 = 0, 0
a1 = 1
while a1 <= x:
ran1 = random.sample(["车", "羊", "羊"], 1)
a1 += 1
if ran1 == ["车"]:
c1 += 1
else:
b1 += 1
gl3 = b1/a1
gl4 = c1/a1
print("换选中车的概率是{:-^20.3f},选中羊的概率是{:-^20.3f}".format(gl3, gl4))
if gl3 > gl:
print("更换选择选中车的概率大!")
elif gl3 < gl:
print("更换选择选中羊的概率大!")
else:
print("更换选择概率是一样大的")
turtle.Screen().setup(800, 800, 150, 150)
move(0, 50)
turtle.hideturtle()
turtle.color('#CD5C5C', 'blue')
turtle.write("不换选中车的概率是{:.2f}".format(gl), font=('Arial', 20, 'bold'), align="center")
move(0, 250)
turtle.hideturtle()
turtle.color('#CD5C5C', 'blue')
turtle.write("换选中车的概率是{:.2f}".format(gl3), font=('Arial', 20, 'bold'), align="center")
turtle.done()
except NameError:
print("格式错误.请重新输入")