Python randomly generates a 6-digit verification code.
1. Example
#-*-Coding: UTF-8-*-import randomdef 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 obtain 6 elements from the list and return verification_code = ''as a piece ''. join (myslice) # list to string # print code_list # print type (myslice) return verification_codedef generate_verification_code2 (): '''randomly generate a 6-digit Verification Code ''' code_list = [] for I in range (2): random_num = random. randint (0, 9) # randomly generate 0-9 numbers # use random. the randint () function generates a random integer a so that 65 <= A <= 90 # Corresponds to the ASCII code a = random from "a" to "Z. randint (65, 90) B = random. randint (97,122) Comment = chr (a) Comment = chr (B) code_list.append (str (random_num) code_list.append (comment) code_list.append (random_lowercase_letter) verification_code = ''. join (code_list) return verification_codeif _ name _ = '_ main _': code = generate_verification_code () code2 = generate_verification_code2 () print code print code2
One of the results is as follows:
GF5UzK
2Cb1Aa