Python generates a 6-digit verification code at random, and python generates a 6-digit verification code.
1. Generate source code
Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-
Import random
Def generate_verification_code ():
''' A 6-digit verification code is randomly generated '''
Code_list = []
For I in range (10): #0-9 Number
Code_list.append (str (I ))
For I in range (65, 91): # A-Z
Code_list.append (chr (I ))
For I in range (97,123): # a-z
Code_list.append (chr (I ))
Myslice = random. sample (code_list, 6) # randomly retrieve 6 elements from the list and return them as a part.
Verification_code = ''. join (myslice) # list to string
# Print code_list
# Print type (myslice)
Return verification_code
Def generate_verification_code2 ():
''' A 6-digit verification code is randomly generated '''
Code_list = []
For I in range (2 ):
Random_num = random. randint (0, 9) # randomly generate 0-9 Numbers
# Use the random. randint () function to generate a random integer a, so that 65 <= a <= 90
# Corresponding ASCII code from "A" to "Z"
A = random. randint (65, 90)
B = random. randint (97,122)
Random_uppercase_letter = chr ()
Random_lowercase_letter = chr (B)
Code_list.append (str (random_num ))
Code_list.append (random_uppercase_letter)
Code_list.append (random_lowercase_letter)
Verification_code = ''. join (code_list)
Return verification_code
If _ name _ = '_ main __':
Code = generate_verification_code ()
Code2 = generate_verification_code2 ()
Print code
Print code2
One of the results is as follows:
Copy codeThe Code is as follows:
GF5UzK
2Cb1Aa