2020-11-06

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               #a是循环定义
    b, c = 0, 0         #b是车的次数 c是羊的次数
    while a <= x:
        ran = random.sample(["车", "羊", "羊"], 1) #模拟选中的是羊还是车
        a += 1
        if ran == ["车"]: #如果选中的是车 那么车的概率+1
            b += 1
        else:           #否则羊+1
            c += 1
    gl = b/a            #这个是选到车的概率
    gl2 = c/a           #这个是选到羊的概率
    print("不换选中车的概率是{:-^20.3f},选中羊的概率是{:-^20.3f}".format(gl, gl2))
    b1, c1 = 0, 0       #b1是车的次数 c1是羊的次数
    a1 = 1              #a1是循环定义
    while a1 <= x:
        ran1 = random.sample(["车", "羊", "羊"], 1)
        a1 += 1
        if ran1 == ["车"]:#如果选中的是车 那么羊的概率+1
            c1 += 1
        else:
            b1 += 1         #如果选中的是羊 那么车的概率+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("格式错误.请重新输入")

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值