• 钻石和玻璃球游戏(钻石位置固定)


     

    '''
    开始,你可以随意选择一个抽屉,在开启它之前,
    主持人会开启另外一个抽屉,露出抽屉里的玻璃球。
    这时,主持人会给你一次更换自己选择的机会。
    请自己认真分析一下“不换选择能有更高的几率获得钻石,
    还是换选择能有更高的几率获得钻石?或几率没有发生变化?”写出你分析的思路和结果。
    设法编写python程序验证自己的想法,
    验证的结果支持了你的分析结果,还是没有支持你的分析结果,
    请写出结果,并附上你的程序代码,并在程序代码中通过注释说明你解决问题的思路。
    (提示:可以借助随机数函数完成此程序)
    '''
    import random
    print("钻石和玻璃球的游戏开始了")
    # 摆在你面前有3个关闭的抽屉
    lst_dic = [{'抽屉':'钻石'},{'抽屉':'玻璃球'},{'抽屉':'玻璃球'}]
    
    # 定义钻石
    zs = 0
    # 定义玻璃球
    blq = 0
    
    def Game(your_choice,lst_dic):
    
        isLouchu = False
        # 查看主持人是否露出
        for num in range(len(lst_dic)):
            if not isLouchu:
                if lst_dic[your_choice]['抽屉'] == '钻石':
                    # 第一种 抽到 钻石
                    if num != your_choice:
                        print("主持人露出了 %d 号抽屉的玻璃球"%(num + 1))
                        isLouchu = True
                else:
                    # 第二种 抽到 玻璃球
                    if num != your_choice and lst_dic[num]['抽屉'] != '钻石':
                        print("主持人露出了 %d 号抽屉的玻璃球"%(num + 1))
                        isLouchu = True
    
        choice = 'yn'
        you_select = random.choice(choice)
        if you_select == 'y':
            lst_nums = [0,1,2]
            ischoose = False
            for new_choice in lst_nums:
                if not ischoose :
                    if (new_choice != num) and (new_choice != your_choice):
                        print("你新的选择是:",new_choice+1,"号抽屉")
                        your_choice = new_choice
    
                        ischoose = True
    
            ChooseLater(your_choice)
    
    
        else:
            print("不变选择,继续坚持我的 %d 号抽屉"%(your_choice + 1))
            your_choice = your_choice
            ChooseLater(your_choice)
    
    def ChooseLater(your_choice):
        # 选择后进行计数  公布答案
        global zs, blq
        if lst_dic[your_choice]['抽屉'] == '钻石':
            zs += 1
            # 钻石数 +1
        else:
            blq += 1
            # 玻璃球数 +1
        answer_num = 0
        isanswer = False
        for answer in lst_dic:
            if not isanswer:
                if answer['抽屉'] == '钻石':
                    print("钻石在 %d 号抽屉 "%(answer_num + 1))
                    isanswer = True
            answer_num += 1
    
    nums = int(input("请输入想要实验的次数"))
    for i in range(nums):
        # 你可以随意选择一个抽屉
        your_choice = random.randint(0, 2)
        print("你当前想要选择的是 %d 号抽屉" % (your_choice + 1))
        Game(your_choice,lst_dic)
    
    print("抽到的钻石数为: %d"%(zs))
    
    print("抽到的玻璃球数为: %d"%(blq))
    
    print("钻石的概率是 %.2f"%(zs/nums))

    2020-05-24

  • 相关阅读:
    python学习之路(目录)--你想要的都在这里了
    计算机视觉学习之路(目录)------你想要的都在这里了
    python flask构建小程序订餐系统--centos下项目开发环境的搭建
    图像处理--图像特效
    数据库--初识数据库
    python基础-面向对象进阶
    JavaScript 模块封装
    JavaScript calss语法糖
    JavaScript 原型与继承
    JavaScript Object对象
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12952401.html
Copyright © 2020-2023  润新知