随机验证码

 1 import random
 2 li = []
 3 for i in range(2):
 4     temp1 = random.randrange(48,58)        # 在48-57中随机产生一个数字
 5     temp2 = random.randrange(65,91)        # 在65-90中随机产生一个数字
 6     temp3 = random.randrange(97,123)       # 在97-122中随机产生一个数字
 7     a = chr(temp1)
 8     b = chr(temp2)       # 将数字转化为ascii码中相对应的字符
 9     c = chr(temp3)
10     li.append(a)
11     li.append(b)         # 将转化后的字符添加到列表li中
12     li.append(c)
13 result = "".join(li)     # join方法将列表li内的值拼接起来
14 print(result)

 上面的程序产生的6位随机验证码中数字和大写字母以及小写字母的位置都是固定的,下面来优化一下,真正做到随机。

 增加一个变量r,在0-4中随机产生一个数字并赋值给变量r,然后用if判断,由于r的值是随机的,所以每次循环随着r的值的改变而执行不同的语句,这样,就可以让数字和大小写字母的位置不再固定不变,真正做到随机。

 1 import random
 2 li = []
 3 for i in range(6):
 4     r = random.randrange(0, 5)
 5     if r == 2:
 6         temp1 = random.randrange(48,58)
 7         c = chr(temp1)
 8         li.append(c)
 9     elif r == 4:
10         temp1 = random.randrange(65,91)
11         c = chr(temp1)
12         li.append(c)
13     else:
14         temp1 = random.randrange(97,123)
15         c = chr(temp1)
16         li.append(c)
17 result = "".join(li)              # join方法将列表li内的值拼接起来,列表中的值必须都是字符串格式
18 print(result)

 我们可以在登陆和注册的程序中加入一个登陆的时候需要输入验证码的功能,代码如下:

 1 # -*- coding:utf-8 -*-
 2 def login(username, password):
 3     """
 4     用于用户登陆
 5     :param username: 用户名
 6     :param password: 密码
 7     :return:
 8     """
 9     f = open('text', 'r')
10     lines = f.readlines()              # 调用文件的readlines方法,读取文件全部内容
11     for line in lines:
12         line_list = line.strip().split('  ')
13         if line_list[0] == username and line_list[1] == password:
14             return True
15     return False
16 
17 
18 def register():
19     """
20     用于用户注册
21     :return:
22     """
23     user = input("请输入您的用户名:")
24     pwd = input("请输入您的密码:")
25     pwd_2 = input("请再次输入您的密码:")
26     if pwd != pwd_2:
27         print("第二次输入的密码和第一次不同,请重新注册!
")
28         register()
29     f = open('text', 'a')
30     temp = '
' + user + '  ' + pwd
31     f.write(temp)
32     f.close()
33     print("注册成功!")
34 
35 
36 def id_code():
37     """
38     产生一个六位的随机验证码
39     :return:
40     """
41     import random
42     li = []
43     for i in range(6):
44         r = random.randrange(0, 5)
45         if r == 2:
46             temp1 = random.randrange(48, 58)
47             c = chr(temp1)
48             li.append(c)
49         elif r == 4:
50             temp1 = random.randrange(65, 91)
51             c = chr(temp1)
52             li.append(c)
53         else:
54             temp1 = random.randrange(97, 123)
55             c = chr(temp1)
56             li.append(c)
57     finilly_code = "".join(li)  # join方法将列表li内的值拼接起来,列表中的值必须都是字符串格式
58     print("33[1;35m %s 33[0m" % finilly_code)
59     user_test = input("请输入验证码(区分大小写):
")
60     if user_test == finilly_code:
61         print("验证码正确!")
62     else:
63         print("验证码错误!")
64         id_code()
65 
66 
67 def main():
68     choice = input("1、登陆  2、注册
")
69     if choice == '1':
70         user = input("请输入您的用户名:")
71         pwd = input("请输入您的密码:")
72         id_code()
73         result = login(user, pwd)
74         if result:
75             print("登陆成功!")
76         else:
77             print("登陆失败,用户名或密码错误!")
78     if choice == '2':
79         register()
80 
81 main()
原文地址:https://www.cnblogs.com/mazhong/p/6139177.html