python年会抽奖小程序
抽奖小程序需求如下:
python代码如下:
标题实现效果图:
问题:
抽奖小程序需求如下:
张三公司有300员工,年会抽奖,奖品如下:
一等奖 3名 外星人笔记本pro一台
二等奖 5名 现金红包10000元
三等奖 10名 小米手机1部
规则:
1、共抽奖3次,第一次抽三等奖,第二次抽二等奖,第三次抽一等奖;
2、每个员工限中奖一次,不能重复
————————————————
代码实现如下:
import random # 导入python模块
userid = list(range(300)) # 生成300个公司员工编号
winer = [] # 创建一个空列表,接收中奖的员工id 检查是否有获奖人重复
choice_number = [] # 以列表长度判断抽奖次数,初始为0次,以及判断是否已经抽过奖
count = 0
while count < 3:
not_winner = userid # 未中奖的员工列表,初始为300,刚开始没有抽奖。
choice = int(input("""
请选择抽奖顺序:
a. 输入3,开始抽取三等奖:
b. 输入2,开始抽取二等奖:
c. 输入1,开始抽取一等奖:
请选择:
"""))
if choice == 3:
if choice in choice_number: # 判断是否已经抽过奖
print(f"已经抽过{choice}等奖了,请抽其他等奖!")
else:
choice_number.append(choice)
first = random.sample(userid, 10) # 三等奖
winer += first # 把中奖的员工编号加到中奖名单列表当中
not_winner = [i for i in not_winner if i in first] # 未中奖的的名单
print(f"恭喜以下员工获得三等奖,小米手机1部!:{first}")
count += 1 # 累计抽奖次数
elif choice == 2:
if choice in choice_number:
print(f"已经抽过{choice}等奖了,请抽其他等奖!")
else:
choice_number.append(choice)
second = random.sample(userid, 5) # 二等奖
winer += second # 把中奖的员工编号加到中奖名单列表当中
not_winner = [i for i in not_winner if i in second] # 未中奖的的名单
print(f"恭喜以下员工获得二等奖,现金红包10000元!:{second}")
count += 1 # 累计抽奖次数
elif choice == 1:
if choice in choice_number:
print(f"已经抽过{choice}等奖了,请抽其他等奖!")
else:
choice_number.append(choice)
third = random.sample(userid, 3) # 一等奖
winer += third # 把中奖的员工编号加到中奖名单列表当中
not_winner = [i for i in not_winner if i in third] # 未中奖的的名单
print(f"恭喜以下员工获得一等奖,外星人笔记本pro一台!:{third}")
count += 1 # 累计抽奖次数
else: # 判断输入是否为抽奖序号,如果不是提示重新输入
print("您输入有误,请重新输入:")
count = len(choice_number) # 判断已经抽了几次奖
本文含有隐藏内容,请 开通VIP 后查看