avatar

10行代码搞定年会抽奖

10行代码搞定年会抽奖

某科技公司有300员工,开年会抽奖,奖项如下:

一等奖 3名
二等奖 6名
三等奖 30名
规则:

1,共抽3次,第一次抽3等奖,第2次抽2等奖,第3次抽1等奖;

2,每个员工限中奖一次,不可重复;

解题思路:

1,生成一个员工列表,用random模块从里面取随机值;

2,取完值之后,立刻从列表中把中奖人删掉,防止其再次中奖。

1
2
3
4
5
6
7
8
9
10
11
12
#年会抽奖程序
import random
staffs = [f"员工-{i}" for i in range(1,301)] #员工列表
levels = [3,6,30] #奖项等级人数
count = 3 #3次循环
while c >0:
choice = input("pree any button to start>>")
winner = random.sample(staffs,levels[c-1]) #取随机数
print(f"{c}等奖:",winner)
for p in winner: #剔除已经中奖人员
staffs.remove(p)
count -=1
运行结果:
1
2
3
4
5
6
pree any button to start>>
3等奖: ['员工-298', '员工-117', '员工-20', '员工-180', '员工-162', '员工-51', '员工-27', '员工-15', '员工-173', '员工-133', '员工-7', '员工-114', '员工-127', '员工-19', '员工-42', '员工-172', '员工-283', '员工-128', '员工-273', '员工-208', '员工-26', '员工-221', '员工-256', '员工-144', '员工-138', '员工-203', '员工-278', '员工-48', '员工-97', '员工-300']
pree any button to start>>
2等奖: ['员工-98', '员工-272', '员工-88', '员工-291', '员工-137', '员工-116']
pree any button to start>>
1等奖: ['员工-53', '员工-238', '员工-25']
文章作者: CodeHao
文章链接: http://codehao.top/ckhoiibzj0000egwo6xft4qx0/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 CodeHao's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论
简体中文