Python generates a 6-digit verification code at random, and python generates a 6-digit verification code.

Source: Internet
Author: User

Python generates a 6-digit verification code at random, and python generates a 6-digit verification code.

1. Generate source code

Copy codeThe Code is as follows:
#-*-Coding: UTF-8 -*-

Import random

Def generate_verification_code ():
''' A 6-digit verification code is randomly generated '''
Code_list = []
For I in range (10): #0-9 Number
Code_list.append (str (I ))
For I in range (65, 91): # A-Z
Code_list.append (chr (I ))
For I in range (97,123): # a-z
Code_list.append (chr (I ))

Myslice = random. sample (code_list, 6) # randomly retrieve 6 elements from the list and return them as a part.
Verification_code = ''. join (myslice) # list to string
# Print code_list
# Print type (myslice)
Return verification_code

Def generate_verification_code2 ():
''' A 6-digit verification code is randomly generated '''
Code_list = []
For I in range (2 ):
Random_num = random. randint (0, 9) # randomly generate 0-9 Numbers
# Use the random. randint () function to generate a random integer a, so that 65 <= a <= 90
# Corresponding ASCII code from "A" to "Z"
A = random. randint (65, 90)
B = random. randint (97,122)
Random_uppercase_letter = chr ()
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 results is as follows:
Copy codeThe Code is as follows:
GF5UzK
2Cb1Aa

Related Article

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.