美文网首页python
Python随机数生成验证码

Python随机数生成验证码

作者: 程序员同行者 | 来源:发表于2018-10-30 15:09 被阅读0次
import random

code = []
#循环生成五个随机数
for i in range(5):
    #当i恰巧等于下面的0-4之间的随机数时,向列表输出一个0-9之间的随机数
    if i == random.randint(0, 4):
        code.append(str(random.randint(0, 9)))
    #否则随机生成一个A-Z之间的字母输出到列表
    else:
        tmp = random.randint(65, 90)
        #chr()函数的作用就是将数字根据ANSII码对应表转换成相应的字母
        code.append(chr(tmp))

print(code)
#将列表转换成字符串
print(''.join(code))

相关文章

网友评论

    本文标题:Python随机数生成验证码

    本文链接:https://www.haomeiwen.com/subject/bxtwtqtx.html