Method One, generate the verification code by choice Way
This method generates each call to Crate_code () generates three random numbers, then selects one of the three random numbers, and the resource call is relatively more
import randomdef V_code (code_length): res = [] if Isinstance (code_length,int): for I in range (code_length): ret = Create_code () res.append (ret) return res else:print ("Enter number of digits in digital form") def Create_code (): #生成随机验证码, by random choice, num = Chr (random.randint ()) #随机数字 Alfs = Chr (random . Randint (+)) #随机大写字母 ALFB = Chr (random.randint (122)) #随机大写字母 s = str (Random.choice ([NUM,ALFS,ALFB])) RET Urn s #方法二 by specifying a choice way to specify # choice = Random.randint (1,3) # If choice = 1: # return Chr (Random.randint ( ) #随机数字 # elif Choice ==2: # return Chr (Random.randint (+)) #随机大写字母 # elif Choice ==3: # RE Turn Chr (random.randint, 122) #随机小写字母if __name__ = = "__main__": Code = V_code (4) for I in range (4): code[ I] = str (code[i]) Code_str = ". Join (Code) print (" Display of array type: ", code) print (" converted to str type: ", code_str)
Method two, generate choice by random
This generates a random number one time based on the choice mode
import randomdef V_code (code_length): res = [] if Isinstance (code_length,int): for I in range (code_length): ret = Create_code () res.append (ret) return res else:print ("Enter number of digits in digital form") def Create_code (): #生成随机验证码, by means of random choice # num = Chr (Random.randint ()) #随机数字 # Alfs = Chr (ra Ndom.randint (+)) #随机大写字母 # ALFB = Chr (Random.randint (, 122)) #随机大写字母 # s = str (Random.choice ([NUM,ALFS,ALFB]) # return S #方法二 by specifying a choice way to specify choice = Random.randint (1,3) If choice = 1:return chr (random.rand Int (elif)) #随机数字 choice ==2:return chr (Random.randint (+)) #随机大写字母 elif Choice ==3:return Chr (Random.randint (122)) #随机小写字母if __name__ = = "__main__": Code = V_code (4) for I in range (4): code[i] = STR (code[i]) Code_str = ". Join (Code) print (" Display of array type: ", code) print (" converted to str type: ", code_str)
Random Module Application-Generate verification code (no picture)