如何基于python實(shí)現(xiàn)年會(huì)抽獎(jiǎng)工具
用python來(lái)實(shí)現(xiàn)一個(gè)抽獎(jiǎng)程序,供大家參考,具體內(nèi)容如下
主要功能有
1.從一個(gè)csv文件中讀入所有員工工號(hào)
2.將這些工號(hào)初始到一個(gè)列表中
3.用random模塊下的choice函數(shù)來(lái)隨機(jī)選擇列表中的一個(gè)工號(hào)
4.抽到的獎(jiǎng)項(xiàng)的工號(hào)要從列表中進(jìn)行刪除,以免再次抽到
初級(jí)版
這個(gè)比較簡(jiǎn)單,缺少定制性,如沒(méi)法設(shè)置一等獎(jiǎng)有幾名,二等獎(jiǎng)有幾名
import csv#創(chuàng)建一個(gè)員工列表emplist = []#用with自動(dòng)關(guān)閉文件with open(’c://emps.csv’) as f: empf = csv.reader(f) for emp in empf: emplist.append(emp)print('進(jìn)行一等獎(jiǎng)抽獎(jiǎng),共有一名')import random#利用random模塊的chice函數(shù)來(lái)從列表中隨機(jī)選取一個(gè)元素e1 = random.choice(emplist)#將中獎(jiǎng)的員工從列表中剔除emplist.remove(e1)print(’一等獎(jiǎng)得主的號(hào)碼是 %s’ % e1)print(’進(jìn)行三個(gè)二等獎(jiǎng)的號(hào)碼抽獎(jiǎng)’)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個(gè)二等獎(jiǎng)是 %s %s %s’,(e2_1,e2_2,e2_3))#下面依次類推可以設(shè)置三等獎(jiǎng)的抽獎(jiǎng)
改進(jìn)版
上面的那個(gè)初級(jí)版,假如要設(shè)置個(gè)三等獎(jiǎng)一百名那么將要重新維護(hù)幾百行代碼,下面用比較高級(jí)點(diǎn)的辦法實(shí)現(xiàn).
我們考慮用面向?qū)ο髞?lái)實(shí)現(xiàn),設(shè)計(jì)一個(gè)抽獎(jiǎng)?lì)悾愔邪粋€(gè)屬性(號(hào)碼來(lái)源),一個(gè)方法:產(chǎn)生所有抽獎(jiǎng)層次指定個(gè)數(shù)的抽獎(jiǎng)號(hào)碼。
用到如下知識(shí)點(diǎn):
1. csv模塊部分函數(shù)用法2. sys模塊讀取輸入3. random模塊函數(shù)choice函數(shù)用法4. 列表和字典元素的添加、刪除6. for循環(huán)中range用法7. 類和面向?qū)ο?. 字符打印,print中的計(jì)算9.open中with
#!/usr/bin/python#coding=utf-8import csvimport sysimport randomreload(sys) sys.setdefaultencoding(’utf8’)#coding=utf-8print('開(kāi)始進(jìn)行抽獎(jiǎng)')#定義個(gè)抽獎(jiǎng)?lì)悾δ苡休斎氤楠?jiǎng)級(jí)別和個(gè)數(shù),打印出每個(gè)級(jí)別的抽獎(jiǎng)員工號(hào)碼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 人參與抽獎(jiǎng)’ % len(emplist)) levels = int(input(’抽獎(jiǎng)分幾個(gè)層次,請(qǐng)輸入:’)) #定義一個(gè)字典 level_dict = {} for i in range(0,levels): print(’請(qǐng)輸入當(dāng)前獲獎(jiǎng)層次 %s 對(duì)應(yīng)的獎(jiǎng)品個(gè)數(shù)’ % ( 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 #循環(huán)完成后抽獎(jiǎng)層次字典構(gòu)造完畢 #進(jìn)行抽獎(jiǎng)開(kāi)始 print(’抽獎(jiǎng)字典設(shè)置為: %s’ % level_dict) for i in range(0,len(level_dict)): winers = [] #產(chǎn)生當(dāng)前抽獎(jiǎng)層次i對(duì)應(yīng)的抽獎(jiǎng)個(gè)數(shù) for j in range(0,int(level_dict[i])): #利用random模塊中的choice函數(shù)從列表中隨機(jī)產(chǎn)生一個(gè) winer = random.choice(emplist) winers.append(winer) emplist.remove(winer) print(’抽獎(jiǎng)層次 %s 下產(chǎn)出的獲獎(jiǎng)人員有:’ % (i + 1 )) print(winers)#類功能定義完畢,開(kāi)始初始化并使用if __name__ == ’__main__’: peoples = Choujiang(’c://emps.csv’) peoples.creat_num()
該段程序在python 2.6 以上及 3中均可以運(yùn)行,運(yùn)行結(jié)果如下
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32Type 'copyright', 'credits' or 'license()' for more information.>>> ================================ RESTART ================================>>> 開(kāi)始進(jìn)行抽獎(jiǎng)共有24790 人參與抽獎(jiǎng)抽獎(jiǎng)分幾個(gè)層次,請(qǐng)輸入:2請(qǐng)輸入當(dāng)前獲獎(jiǎng)層次 1 對(duì)應(yīng)的獎(jiǎng)品個(gè)數(shù)1請(qǐng)輸入當(dāng)前獲獎(jiǎng)層次 2 對(duì)應(yīng)的獎(jiǎng)品個(gè)數(shù)3抽獎(jiǎng)字典設(shè)置為: {0: 1, 1: 3}抽獎(jiǎng)層次 1 下產(chǎn)出的獲獎(jiǎng)人員有:[[’張三19826’]]抽獎(jiǎng)層次 2 下產(chǎn)出的獲獎(jiǎng)人員有:[[’張三18670’], [’張三23235’], [’張三15705’]]>>>
:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
