码迷,mamicode.com
首页 > 其他好文 > 详细

生成随机验证码

时间:2018-04-20 22:12:04      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:字符   family   pre   str   方法   mic   range   生成   整数   

#coding=utf-8

方法一
import random
checkcode=‘‘
for i in range(4): #0,1,2,3
current=random.randrange(0,4)
if current !=i:
temp=chr(random.randint(65,90)) #65以上是字母,chr()把整数作为参数,返回一个对应的字符
else:
temp=random.randint(0,9)
checkcode +=str(temp)
print checkcode



方法二
import random
checkcode=[]
for i in range(4):
if i==random.randint(1,3):
checkcode.append(str(i))
else:
temp=chr(random.randint(65,90))
checkcode.append(str(temp))
print checkcode
print ‘‘.join(checkcode)

生成随机验证码

标签:字符   family   pre   str   方法   mic   range   生成   整数   

原文地址:https://www.cnblogs.com/hyy0927/p/8893260.html

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