A company has 300 employees, and the requirements for the annual meeting lottery are as follows:
3 first prizes, prize xxx
Level 2 6 winners, prize xxxx
Level 30, prize xxx
Require:
1. A total of 3 draws, the first draw for the third grade, the second draw for the second prize, and the third draw for the first prize
2. Each employee is limited to winning once and cannot repeat
I just wrote it as follows: If there is something wrong or a better method, please give me advice~
import random #导入random模块用于生成随机数
employee = list(range(300))#生成300个员工id
winner_list = []#定义一个初始化获奖名单
for i in range(3):#抽奖3次设置3次循环
# ======3等奖====
if i == 1:
t3 = random.sample(employee, 30)#使用random.sample生成30个随机数
if t3 not in winner_list:#判断如果30个随机数不在获奖名单中
print(f"恭喜编号为", random.sample(employee, 30), "的员工获得了三等奖==大润发100元代金券")#输出获奖名单
winner_list.append(t3)#将获奖名单的员工编号存进获奖名单中
# =====2等奖=====
elif i == 2:
t2 = random.sample(employee, 6)
if t2 not in winner_list:
print(f"恭喜编号为", random.sample(employee, 6), "的员工获得了二等奖===小米手机一部")
winner_list.append(t2)
# ======1等奖=====
else:
t1 = random.sample(employee, 3)
if t1 not in winner_list:
print(f"恭喜编号为", random.sample(employee, 3), "的员工获得了一等奖===mac book pro一台")
winner_list.append(t1)
break
The execution results are as follows