模拟轮盘抽奖游戏:一等奖、二等奖、三等奖

模拟轮盘抽奖游戏:一等奖、二等奖、三等奖。轮盘随机转动:一等奖范围[0, 0.08];二等奖范围[0.08, 0.3];三等奖范围[0.3, 1]。模拟本次活动1000人参加,模拟游戏需要准备个等级评奖的个数。

import random
level = {
    '一等奖':(0,0.08),
    '二等奖':(0.08,0.3),
    '三等奖':(0.3,1)
}
def user_lv():
    num = random.random()
    for k,v in level.items():
        if v[0]<= num < v[1]:
            return k
res_lv = {}
for i in range(1000):
    res = user_lv()
    if res not in res_lv:
        res_lv[res] = 1
    else:
        res_lv[res] += 1
for k,v in res_lv.items():
    print(k,':',v)

你可能感兴趣的:(educoder,python)