Each person generates a corresponding lottery code;
Begin drawing lots;
Announce the winning result;
The writing of this case is relatively simple. The actual production environment is much more complicated than this. However, the basic logic is still the same...
import random
def get_random_code(num_code):"""
Generate and return a random verification code
: param num_code:Random verification code digits
"""
list_code =[]for i inrange(num_code):
# (1) Randomly generate three kinds of characters
# (1-1) Randomly generate 0~Number 9
num =str(random.randint(0,9))
# (1-2) Randomly generate lowercase letters
lower_alphabet =chr(random.randint(97,122))
# (1-3) Randomly generate uppercase letters
upper_alphabet =chr(random.randint(65,90))
# (2) Randomly sample a character again
random_code = random.choice([num, lower_alphabet, upper_alphabet])
# (3) Save randomly selected characters
list_code.append(random_code)return"".join(list_code)
def main():
# Total number of draws
draw_num =30
# Total number of winners
draw_result_num =10
# List of total lottery codes
draw_list =[]
# List of winning lottery codes
draw_result_list =[]
# (1) Simulate to generate the lottery code. Actually, everyone should get the lottery code by themselves. Now use the loop to simulate
for i inrange(draw_num):
draw_code =get_random_code(6)while draw_code in draw_list:
# Repeat label, need to regenerate lottery code
draw_code =get_random_code(6)else:
# Add the generated tag code to the list
draw_list.append(draw_code)
# (2) Simulate the process of drawing lots
print("Produced a total of{}Lottery codes:{}".format(draw_num, draw_list))for i inrange(draw_result_num):
# NO1:In this way, a total of the generated lottery codes will be deleted, so other methods are still used
# 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:
# Duplicate labels and draw lots
draw_code = draw_list[random.randint(a, b)]else:
# Add the lottery code to the list
draw_result_list.append(draw_code)
# (3) Announce the lottery results
print("{}The lottery codes for the winning lottery are as follows:".format(draw_result_num))for item in draw_result_list:print(item)if __name__ =='__main__':main()
The console output:
A total of 30 lottery codes were generated: ['511uCZ','nAYBf8', '6I92sq', '206FxX', '1WMHCb','sdJUMU','e4A85p', '14Ae35','n4xA44', '94i4eG', ' L8981K', '11wtyN','i5k5Xk', '7S8Tf7','cGC4Ku','f8dM40','nBi958','ELDVqz','MQZFJQ','hPPqh5','Qr682E','E2Z4X4J','Ilm ,'J71Pc1','JPZpv8','svAlm3', '9GaS9U','v8378K', '116u2b','x688xP']
10 The lottery codes for the winning lottery are as follows:
i5k5Xk
v8378K
e4A85p
J71Pc1
hPPqh5
1 WMHCb
JPZpv8
sdJUMU
9 GaS9U
MQZFJQ
So far, this article about the function of python3 to implement mask lottery is introduced. For more related python3 mask lottery content, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future!
Recommended Posts