需求
需要一个摇号系统 40人摇摊位 人员确定 其中部分人员需要了多个摊位 摊位号按照顺序排列显示
比如说张三需要一个摊位,李四需要两个,王五需要一个。 第一次摇出来王五,屏幕显示“1号摊位” 第二次摇出李四,屏幕显示“2、3号摊位”。
代码实现
import random
import csv
class LotterySys(object):
def __init__(self):
self.con_list = []
self.shops_num = []
def get_con_list(self):
csv_file = csv.reader(open("摇号.csv", "r"))
for fi in csv_file:
if fi[0] == "序号":
continue
else:
fi[4] = list(fi[4].split(","))
while '' in fi[4]:
fi[4].remove('')
self.con_list.append(fi)
def get_shops_num(self):
num = 0
for x in self.con_list:
num = num + int(x[2])
for i in range(1,num+1):
self.shops_num.append(str(i))
def range_get_people(self):
count = len(self.con_list)
for i in range(count):
num = random.randint(0,len(self.con_list)-1)
need_shops = int(self.con_list[num][2])
get_shops = self.shops_num[0:need_shops]
# print(type(get_shops))
# print(get_shops)
info = "姓名:{},分配店铺数:{},铺号:{}".format(self.con_list[num][1],need_shops,",".join(get_shops))
# .format(self.con_list[num][1]),need_shops,get_shops)
print(info)
del self.con_list[num]
del self.shops_num[0:need_shops]
def run(self):
self.get_con_list()
self.get_shops_num()
self.range_get_people()
if __name__ == '__main__':
ls = LotterySys()
ls.run()
序号,姓名,需要店铺数,是否锁定,锁定铺号
1,糖一,2,0,
2,猴二,1,0,
3,朱三,3,0,
4,傻四,2,0,
5,马五,3,0,"1,2,3"
6,牛一,1,0,
7,关二,1,0,
8,张三,2,0,
9,赵四,1,0,
10,魏球,2,0,