python3 口罩抽签 python3 实现口罩抽签的功能
Commas.KM 人气:0一、口罩抽签的逻辑
每个人产生对应的抽签码;
开始进行抽签;
公布中签结果;
这次案例写的比较简单,实际生产环境远比这复杂的多,但是万变不离其宗,基本逻辑还是差不多的……
二、口罩抽签的代码
import random def get_random_code(num_code): """ 生成并返回随机验证码 :param num_code: 随机验证码位数 """ list_code = [] for i in range(num_code): # (1)随机生成三种字符 # (1-1)随机生成0~9的数字 num = str(random.randint(0, 9)) # (1-2)随机生成小写字母 lower_alphabet = chr(random.randint(97, 122)) # (1-3)随机生成大写字母 upper_alphabet = chr(random.randint(65, 90)) # (2)再次随机抽取一个字符 random_code = random.choice([num, lower_alphabet, upper_alphabet]) # (3)保存随机抽取的字符 list_code.append(random_code) return "".join(list_code) def main(): # 抽签总人数 draw_num = 30 # 中签的总人数 draw_result_num = 10 # 总抽签码列表 draw_list = [] # 中签的抽签码列表 draw_result_list = [] # (1)模拟生成抽签码,实际应该是每个人自己获取抽签码,现在用循环来模拟 for i in range(draw_num): draw_code = get_random_code(6) while draw_code in draw_list: # 重复标签,需要重新生成抽签码 draw_code = get_random_code(6) else: # 将生成的标签码加入列表 draw_list.append(draw_code) # (2)模拟产生抽签的过程 print("总共产生了{}个抽签码:{}".format(draw_num, draw_list)) for i in range(draw_result_num): # NO1:这种方式会总共生成的抽签码给删除,所以还是采用其它方式 # random_index = random.randint(0, len(draw_list)-1) # draw_result_list.append(draw_list.pop(random_index)) # NO2: a = 0 b = len(draw_list) - 1 draw_code = draw_list[random.randint(a, b)] while draw_code in draw_result_list: # 重复标签,重新抽签 draw_code = draw_list[random.randint(a, b)] else: # 将抽中的抽签码加入列表 draw_result_list.append(draw_code) # (3)公布抽签结果 print("{}位中签的抽签码如下:".format(draw_result_num)) for item in draw_result_list: print(item) if __name__ == '__main__': main()
控制台输出结果:
总共产生了30个抽签码:[‘511uCZ', ‘nAYBf8', ‘6I92sq', ‘206FxX', ‘1WMHCb', ‘sdJUMU', ‘e4A85p', ‘14Ae35', ‘n4xA44', ‘94i4eG', ‘L8981K', ‘11wtyN', ‘i5k5Xk', ‘7S8Tf7', ‘cGC4Ku', ‘f8dM40', ‘nBi958', ‘ELDVqz', ‘MQZFJQ', ‘hPPqh5', ‘Qr682E', ‘E2Z4X4', ‘IlmRJJ', ‘J71Pc1', ‘JPZpv8', ‘svAlm3', ‘9GaS9U', ‘v8378K', ‘116u2b', ‘x688xP']
10位中签的抽签码如下:
i5k5Xk
v8378K
e4A85p
J71Pc1
hPPqh5
1WMHCb
JPZpv8
sdJUMU
9GaS9UMQZFJQ
加载全部内容