简单随机验证码生成

random 模块学习

纯数字4位验证码:

1 import random
2 checkcode=''
3 for i in range(4):
4     current=random.randint(1,9)
5     checkcode+=str(currenmt)
6 print (checkcode)

数字+大写字母 4位验证码

 1 import random
 2 checkcode=''
 3 for i in range(4):
 4     current=random.randint(1,4)
 5     if  current=i:
 6             tmp=chr(random.randint(65,90))
 7     else:
 8             tmp=random.randint(0,9)
 9     checkcode+=str(tmp)
10 print (checkcode)

数字+小写字母  4位验证码

 1 import random
 2 checkcode=''
 3 for i in range(4):
 4     current=random.randint(0,4)
 5     if current=i:
 6         tmp=chr(random.randint(97,122))
 7     else:
 8         tmp=random.randint(0,9)
 9     chechcode+=str(tmp)
10 print (checkcode)

数字+大写+小写 4位验证码:

 1 import random
 2 checkcode=''
 3 for i in range(0,4):
 4     current=random.randint(0,4)
 5     if current == 1:
 6         tmp=chr(random.randint(64,90))
 7     elif current ==2:
 8         tmp = chr(random.randint(97.122))
 9     else :
10         tmp=random.randint(0,9)
11         checkcode+=str(tmp)
12 print (checkcode)

用 if进行判断。可以多种验证码。

原文地址:https://www.cnblogs.com/pangya/p/10071761.html