生成随机验证码

#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)
posted @ 2018-04-20 19:46  hyy0927  阅读(111)  评论(0编辑  收藏  举报