1. Generate source
Copy Code code as follows:
#-*-Coding:utf-8-*-
Import Random
Def generate_verification_code ():
"' randomly generated 6-bit verification code '"
Code_list = []
For I in range (10): # 0-9 digits
Code_list.append (str (i))
For I in range ($): # A-Z
Code_list.append (Chr (i))
For I in range (123): # A-Z
Code_list.append (Chr (i))
myslice = Random.sample (code_list, 6) # randomly get 6 elements from list, return as a fragment
Verification_code = '. Join (myslice) # List to string
# print code_list
# PR int type (myslice)
return Verification_code
Def generate_verification_code2 ():
' randomly generated 6-bit verification code '
code_list = []
for I in range (2):
random_num = random.randint (0 , 9) # Randomly generated 0-9 of the number
# using the Random.randint () function to generate a random integer a, making 65<=a<=90
# to ASCII code from "a" to "Z"
A = Random.randint
b = random.randint (122)
& nbsp; random_uppercase_letter = Chr (a)
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 resulting results is as follows:
Copy Code code as follows: