一,问题描述
某公司有员工300名,开年会抽奖,,其中一等奖三名,二等奖6名,三等奖30名
规则:
1,一共抽三次,第一次抽三等级奖,第二次抽二等奖,第三次抽一等奖
2,每个员工限中奖一次,不能重复
二,解题思路
1,生成一个员工列表,使用random模块从中选取随机值
2,取完值之后,立刻把其从员工列表中删除,即可防止其再次中奖
三,代码实现
import random
employees=[]
for i in range(300):
employee=f"员工{i}"
employees.append(employee)
#生成一个300名员工的列表
count=0
while(count<3):
if count==0:
#第一次循环抽三等奖
for j in range(30):
employee=random.choice(employees)
print(f"恭喜{employee}获得三等奖")
employees.remove(employee)#抽中谁把谁从列表中移除
print("\n")
#第二次循环抽二等奖
if count==1:
for k in range(6):
employee = random.choice(employees)
print(f"恭喜{employee}获得二等奖")
employees.remove(employee)
print("\n")
#第三次循环抽一等奖
if count==2:
for m in range(3):
employee = random.choice(employees)
print(f"恭喜{employee}获得一等奖")
employees.remove(employee)
print("\n")
count+=1