Python - random库使用例子——生成随机验证码


用于生成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'错误提示,后来文件名改为“random1”就可以了


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM