码迷,mamicode.com
首页 > 编程语言 > 详细

Python random随机生成6位验证码示例代码

时间:2020-06-09 09:55:45      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:ring   imp   type   The   dom   integer   app   __name__   sci   

随机生成6位验证码代码

# -*- coding: utf-8 -*-
import random
def generate_verification_code():
    ‘‘‘ randomly generated a 6 bit verification code ‘‘‘
    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)  
    verification_code = ‘‘.join(myslice) # list to string
    # print code_list
    # print type(myslice)
    return verification_code
def generate_verification_code2():
    ‘‘‘ randomly generated a 6 bit verification code ‘‘‘
    code_list = []
    for i in range(2):
        random_num = random.randint(0, 9) # The number of randomly generated 0-9
       # using random.randint () function to generate a random integer a, such that 65<=a<=90
       # corresponding from "A" to "Z" of the ASCII code
        a = random.randint(65, 90)
        b = random.randint(97, 122)
        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

执行结果

fF3UzK 
1Db2Aa

原文地址https://www.cjavapy.com/article/25/

Python random随机生成6位验证码示例代码

标签:ring   imp   type   The   dom   integer   app   __name__   sci   

原文地址:https://www.cnblogs.com/coderliang/p/13070369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!