zoukankan      html  css  js  c++  java
  • 钻石和玻璃球游戏(钻石位置不固定)

    import random
    
    def no_change(times):
        glass_ball = 0
        diamond = 0
        times2=times
        while times>0:
            drawers={'drawer1': False, 'drawer2': False, 'drawer3': False}
            drawer_with_diamond=random.randint(1,3)  #获取随机数1-3
            drawers['drawer%d'%drawer_with_diamond]=True  #随机在3个抽屉中放入钻石
            your_choice=random.randint(1,3) #再次获取随机数1-3
            what_you_get=drawers['drawer%d'%your_choice]
            if what_you_get==False:  #判定选到的是什么
                glass_ball+=1
            else:
                diamond+=1
            times-=1
        winning_rate=diamond/(diamond+glass_ball)  #获取赢得钻石的概率
        print('一共试验了%d次,你获得钻石的概率为%f'%(times2, winning_rate))
    
    def change_drawer(times):
        glass_ball = 0
        diamond = 0
        times2=times
        while times>0:
            drawers = {'drawer1': False, 'drawer2': False, 'drawer3': False}
            drawer_with_diamond = random.randint(1, 3)  # 获取随机数1-3
            drawers['drawer%d' % drawer_with_diamond] = True  # 随机在3个抽屉中放入钻石
            your_choice = random.randint(1, 3)  # 再次获取随机数1-3
            your_drawer='drawer'+str(your_choice)
            for things in drawers:
                if drawers[things] == True: #获取钻石在第几个抽屉中
                    if things==your_drawer: #如果此时你选的,正是有钻石的那个抽屉,并且你换抽屉,那么你必然拿到玻璃球
                        glass_ball+=1
                    if things!=your_drawer: #如果此时你选的,是有玻璃球的,主持人给你展示另一个有玻璃球的的,你换抽屉,必然拿到钻石
                        diamond+=1
            times-=1
        winning_rate=diamond/(diamond+glass_ball)  #获取赢得钻石的概率
        print('一共试验了%d次,你获得钻石的概率为%f' % (times2, winning_rate))
    
    def main():
        change_or_not=str(input('你选择换还是不换抽屉?换/不换:'))
        if change_or_not=='':
            times=int(input('请输入你想试验的次数:'))
            change_drawer(times)
        else:
            times = int(input('请输入你想试验的次数:'))
            no_change(times)
    
    if __name__=='__main__':
        main()

    2020-05-27

  • 相关阅读:
    python的特点
    epoll理解(转)
    数据库存储过程、触发器、连接
    Mysql的四种隔离级别
    linux指令
    利用asyncio(支持异步io)和协程实现单线程同步
    ubuntu安装codeblocks
    临界区与互斥量区别
    单链表的简单操作
    hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12971307.html
Copyright © 2011-2022 走看看