Python randomly generates a 6-bit CAPTCHA code sharing _python

Source: Internet
Author: User

1. Generate source

Copy Code code as follows:

#-*-Coding:utf-8-*-

Import Random

Def generate_verification_code ():
"' randomly generated 6-bit verification code '"
Code_list = []
For I in range (10): # 0-9 digits
Code_list.append (str (i))
For I in range ($): # A-Z
Code_list.append (Chr (i))
For I in range (123): # A-Z
Code_list.append (Chr (i))

    myslice = Random.sample (code_list, 6)   # randomly get 6 elements from list, return as a fragment
    Verification_code = '. Join (myslice) # List to string
    # print code_list
    # PR int type (myslice)
    return Verification_code

Def generate_verification_code2 ():
    ' randomly generated 6-bit verification code '
    code_list = []
    for I in range (2):
        random_num = random.randint (0 , 9) # Randomly generated 0-9 of the number
        # using the Random.randint () function to generate a random integer a, making 65<=a<=90
        # to ASCII code from "a" to "Z"
        A = Random.randint
        b = random.randint (122)
  & nbsp;     random_uppercase_letter = Chr (a)
        Random_lowercase_letter = Chr (b)

Code_list.append (str (random_num))
Code_list.append (Random_uppercase_letter)
Code_list.append (Random_lowercase_letter)
Verification_code = '. Join (Code_list)
Return Verification_code

if __name__ = = ' __main__ ':
Code = Generate_verification_code ()
Code2 = Generate_verification_code2 ()
Print Code
Print Code2

One of the resulting results is as follows:

Copy Code code as follows:

Gf5uzk
2cb1aa

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.