用python来实现一个抽奖程序,供大家参考,具体内容如下

主要功能有

1.从一个csv文件中读入所有员工工号
2.将这些工号初始到一个列表中
3.用random模块下的choice函数来随机选择列表中的一个工号
4.抽到的奖项的工号要从列表中进行删除,以免再次抽到

初级版

这个比较简单,缺少定制性,如没法设置一等奖有几名,二等奖有几名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import csv
#创建一个员工列表
emplist = []
#用with自动关闭文件
with open('c://emps.csv') as f:
 empf = csv.reader(f)
 for emp in empf:
  emplist.append(emp)
print("进行一等奖抽奖,共有一名")
import random
#利用random模块的chice函数来从列表中随机选取一个元素
e1 = random.choice(emplist)
#将中奖的员工从列表中剔除
emplist.remove(e1)
print('一等奖得主的号码是 %s' % e1)
print('进行三个二等奖的号码抽奖')
e2_1 = random.choice(emplist)
emplist.remove(e2_1)
e2_2 = random.choice(emplist)
emplist.remove(e2_2)
e2_3 = random.choice(emplist)
emplist.remove(e2_3)
print('获得3个二等奖是 %s %s %s',(e2_1,e2_2,e2_3))
#下面依次类推可以设置三等奖的抽奖

改进版

上面的那个初级版,假如要设置个三等奖一百名那么将要重新维护几百行代码,下面用比较高级点的办法实现.

我们考虑用面向对象来实现,设计一个抽奖类,类中包含一个属性(号码来源),一个方法:产生所有抽奖层次指定个数的抽奖号码。

用到如下知识点:

1. csv模块部分函数用法
2. sys模块读取输入
3. random模块函数choice函数用法
4. 列表和字典元素的添加、删除
6. for循环中range用法
7. 类和面向对象
8. 字符打印,print中的计算
9.open中with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
#coding=utf-8
import csv
import sys
import random
reload(sys)
sys.setdefaultencoding('utf8')
#coding=utf-8
print("开始进行抽奖")
#定义个抽奖类,功能有输入抽奖级别和个数,打印出每个级别的抽奖员工号码
class Choujiang:
 #定义scv文件路径
 def __init__(self,filepath):
  self.empfile = filepath
 def creat_num(self):
  emplist = []
  with open(self.empfile) as f:
   empf = csv.reader(f)
   for emp in empf:
    emplist.append(emp)
  print('共有%s 人参与抽奖' % len(emplist))
  levels = int(input('抽奖分几个层次,请输入:'))
  #定义一个字典
  level_dict = {}
  for i in range(0,levels):
   print('请输入当前获奖层次 %s 对应的奖品个数' % ( i + 1))
   str_level_dict_key = sys.stdin.readline()
   int_level_dict_key = int(str_level_dict_key)
   level_dict[i] = int_level_dict_key
   #循环完成后抽奖层次字典构造完毕
  #进行抽奖开始
  print('抽奖字典设置为: %s' % level_dict)
  for i in range(0,len(level_dict)):
   winers = []
   #产生当前抽奖层次i对应的抽奖个数
   for j in range(0,int(level_dict[i])):
    #利用random模块中的choice函数从列表中随机产生一个
    winer = random.choice(emplist)
    winers.append(winer)
    emplist.remove(winer)
   print('抽奖层次 %s 下产出的获奖人员有:' % (i + 1 ))
   print(winers)
#类功能定义完毕,开始初始化并使用
if __name__ == '__main__':
 peoples = Choujiang('c://emps.csv')
 peoples.creat_num()

该段程序在python 2.6 以上及 3中均可以运行,运行结果如下图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
开始进行抽奖
共有24790 人参与抽奖
抽奖分几个层次,请输入:2
请输入当前获奖层次 1 对应的奖品个数
1
请输入当前获奖层次 2 对应的奖品个数
3
抽奖字典设置为: {0: 1, 1: 3}
抽奖层次 1 下产出的获奖人员有:
[['张三19826']]
抽奖层次 2 下产出的获奖人员有:
[['张三18670'], ['张三23235'], ['张三15705']]
>>>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我。

最新文章

  1. iOS视频播放器
  2. SpringMVC 结合HttpClient调用第三方接口实现
  3. Python基本数据类型之dict
  4. <<卸甲笔记>>-Oracle线下迁移到PPAS
  5. Binary Tree Level Order Traversal [LeetCode]
  6. p1205单词翻转-递归解决
  7. 在Linux下安装C/C++开发工具包的最佳方式
  8. linux netstat 命令详解
  9. Android获取手机屏幕宽高
  10. IZT复杂电磁环境记录回放和模拟系统
  11. 题解 P4692 【[Ynoi2016]谁的梦】
  12. Django与Celery配合实现定时任务
  13. rem 原理与简介
  14. MySql 三大知识点——索引、锁、事务
  15. C#常用代码(更新中)
  16. [Full-stack] 异步即时通信 - Async
  17. SpringBoot @Aspect
  18. mysql主从复制-方案1
  19. java中集合格式及json格式的特点和转换
  20. asp.net Core 中间件Hello world

热门文章

  1. 【题解】SP10570 【LONGCS - Longest Common Substring】
  2. 小程序将base64的多张图片,传到tp5后台
  3. linux启动过程中建立临时页表
  4. 多测师讲解jmeter _接口请求_(003)高级讲师肖sir
  5. 第一月多测师讲解_ linux_vim命令_004
  6. 使用notepad++的nppexec插件格式化json和压缩json内容
  7. 使用python编写正逆序乘法表
  8. 【值得收藏】C语言入门基础知识大全!从C语言程序结构到删库跑路!
  9. Linux安装软件时90%的人会遇到这个报错,如何解决?
  10. 【转】Python 魔法方法大全