商城系统 注册

年会抽奖小程序源码分析

2018-02-05|HiShop
导读:小程序开发大热,今年许多公司都想到用小程序实现抽奖,下面为大家介绍年会抽奖小程序源码分析。...

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

  用到如下知识点:

  1. csv模块部分函数用法

  2. sys模块读取输入

  3. random模块函数choice函数用法

  4. 列表和字典元素的添加、删除

  6. for循环中range用法

  7. 类和面向对象

  8. 字符打印,print中的计算

  9.open中with

  #!/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()

  1234567891011121314151617181920212223242526272829303132333435363738394041424344454647

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

  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']]

  >>>

 

 HiShop小程序工具提供多类型商城/门店小程序制作,可视化编辑 1秒生成5步上线。通过拖拽、拼接模块布局小程序商城页面,所看即所得,只需要美工就能做出精美商城。

更多小程序资讯,尽在:www.hishop.com.cn/xiaocx/

电话咨询 预约演示 0元开店