Python编程—模拟轮盘游戏

题目要求:

模拟轮盘抽奖游戏
# 轮盘分为三部分: 一等奖, 二等奖和三等奖;
# 轮盘转的时候是随机的,
#       如果范围在[0,0.08)之间,代表一等奖,
#       如果范围在[0.08,0.3)之间,代表2等奖,
#       如果范围在[0.3, 1.0)之间,代表3等奖,
# 模拟本次活动1000人参加, 模拟游戏时需要准备各等级奖品的个数.
"""
import random

rewardDict = {
    '一等奖':(0,0.08),
    '二等奖':(0.08,0.3),
    '三等奖':(0.3,1.0)
}

def rewardFun():
    """用户得奖等级"""
    #生成一个0~1之间的随机数
    num = random.random()
    #判断随机转盘转的是几等奖
    for k,v in rewardDict.items():
        if v[0] <= num < v[1]:
            return k

resultDict = {}

for i in range(1000):
    res = rewardFun()
    if res not in resultDict:
        resultDict[res] = 1
    else:
        resultDict[res] += 1

for k,v in resultDict.items():
    print(k,'------>',v)

猜你喜欢

转载自blog.csdn.net/weixin_43314056/article/details/86556383