【python开发】写一个验证码生成和检验程序

这是一个随机生成四位字母和数字混合验证码, 并且进行输入验证的程序,和我们平时用到的验证码原理相同。

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 def check_code():#定义一个验证码生成程序
 4     import random#引入random
 5     checkcode=' '
 6     for i in range(4):
 7         count = random.randrange(0,4)
 8         if count!=i:
 9             temp=chr(random.randint(65,90))
10         else:
11             temp=random.randint(0,9)
12         checkcode+=str(temp)
13     return checkcode
14 while True:#生成一个循环验证程序
15     code=check_code()
16     print(code)
17     a=code.lower()
18     b=a.lstrip()#对验证码左边空白缩进
19     v=input("please input the code:")
20     if v.lower()!=b:
21         print("error")
22     else:
23         print("correct")
24         break

 

posted on 2018-10-16 09:01  于兴  阅读(2402)  评论(0编辑  收藏  举报