1. 程式人生 > >生成隨機驗證碼

生成隨機驗證碼

emp OS [] () 隨機 style odin utf str

#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)

生成隨機驗證碼