用于生成4位随机验证码

#_*_coding:utf-8_*_
#__author__ = "csy"
import random
checkcode=''
for i in range(4):
    current=random.randrange(0,4)   #生成随机数与循环次数比对
    current1 = random.randrange(0,4)
    if current == i:
        tmp=chr(random.randint(65,90))   #65~90为ASCii码表A~Z
    elif current1 == i:
        tmp = chr(random.randint(97,122))   #97~122为ASCii码表a~z
    else:
        tmp=random.randint(0,9)
    checkcode+=str(tmp)
print(checkcode)

 注意该python文件名为“random”,运行时可能会出现 AttributeError: module 'random' has no attribute 'randrange'错误提示,因为random本身是一个内置的模块与本python文件重名了,后来文件名改为“random1”就可以了

生成6位随机验证码

import random

def v_code():
    code=''
    for i in range(6):
        if random.randint(0,1) == 0:
            add = random.randrange(10)
        else:
            add=chr(random.randrange(65,91))
        code+=str(add)
    print(code)

v_code()

 

posted on 2017-07-16 18:09  csy113  阅读(2540)  评论(1编辑  收藏  举报