Random module application-Generate verification code (no picture)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.