Simplified verification code functionality in Python

Source: Internet
Author: User

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

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.