1 #! /usr/bin/env python2 #_*_ encoding:utf-8 _*_3 #author:snate4 ImportRandom5 defGenerate_auth_code ():6Auth_code_list =[]7 forIinchRange (6):#to generate several verification code loops several times8num = Random.randint (0,5)#randomly generate numbers from 0 to 59 ifNum ==1ornum = = 4:#If the generated number is 1, 4, generate 0-9 of the numberTencode1= Random.randrange (0,9)# OneAuth_code_list.append (str (CODE1))#Add verification Code list A Else: -Code2 = Random.randrange (65,90)#If the generated number is not 1,4 generates a number between 65 and 90, and translates into the corresponding ASCII code. -Code2 =chr (code2). Lower () theAuth_code_list.append (Code2)#adding letters to the list of verification codes - return "". Join (Auth_code_list)#Convert a list into a string - Print(Generate_auth_code ())
The basic principle is to randomly generate numbers using random:
1. Several characters to loop several times
2. Randomly generate a number that defines if the number is one of the values X, then randomly a 0-9 median number. Why 0-9, because 10 of the words accounted for two characters ....
3. In the loop, if the random number is not X, generate a number between 65-90, and then use the char () function to convert to the corresponding letter in the ASCII list, which is, of course, the uppercase A-Z, and then convert the uppercase Chen Xiao to write.
4. Add the generated characters to the list, and finally stitch the list into a string to O,
Simplified verification code functionality in Python