Python3 编写登陆接口

题目选自 Alex Python自动化开发之路的题目,我是从C++转学Python的,编写的水平有限,轻喷。

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

首先应该有2个txt文件,包含用户名密码的user.txt和包含被锁定文件的lock.txt(用户名自己设定即可)

我的user.txt文件每行有一个用户名和密码 中间用空格隔开

类似: 

zhu 123456

zhang 123456

lock.txt每行有一个用户名

代码如下:

 1 # Author:Zhu
 2 
 3 count = 0
 4 while count < 3:
 5     username = input("请输入用户名: ")
 6     if username == '':
 7         exit("用户名输入错误,程序退出...")
 8     lock_file = open("lock.txt", 'r')
 9     lockf = lock_file.readlines()
10     # 锁定账号的比对
11     for line in lockf:
12         if username in line.split():  # 注意换行符的分割
13             exit("\033[31;1m你的账户[%s]已经被锁定,请联系管理员\033[0m" %username)
14     lock_file.close()
15     password = input("请输入密码:")
16     login_check = open('user.txt', 'r')
17     loginf = login_check.readlines()
18     # 登录账号和密码的对比
19     for line in loginf:
20         line = line.split()
21         if username == line[0] and password == line[1]:
22             exit("\033[32;1m登录成功!欢迎您,%s\033[0m" %username)
23         elif username == line[0] and password != line[1]:
24             pass_count = 0
25             while pass_count < 3:
26                 print("\033[31;1m您的密码错误,剩余尝试次数[%s]次\033[0m" %(2-pass_count))
27                 password_r = input("请重新输入密码:")
28                 pass_count += 1
29                 if password_r == line[1]:
30                     exit("\033[32;1m登录成功!欢迎您,%s\033[0m" % username)
31                 if pass_count == 2:
32                     print("\033[31;1m你的密码输入错误次数已经达到最大,账号[%s]被锁定\033[0m" %username)
33                     in_lock = open("lock.txt", 'a+')
34                     in_lock.write(username)
35                     in_lock.write('\n')
36                     in_lock.close()
37                     exit()
38 
39 
40     else:
41         print("您的账号[%s]不存在,请注册" %username)
42 
43     count += 1
44     if count == 3:
45         print("您已经尝试3次,登录失败")
46     else:
47         print("请重新输入:")

为了配合登录界面功能的实现,我又写了一个注册界面的编写

 1  Author:Zhu
 2 
 3 
 4 
 5 def username_check(username):
 6     while True:
 7         file_check = open('user.txt', 'r')
 8         for line in file_check.readlines():
 9             line = line.split()
10             if username == line[0]:
11                 print("账号已经存在,请重新输入账号:")
12                 return 0
13         else:
14             print("可以注册")
15             return 1
16             break
17 
18 
19 while True:
20     username = input("请输入您的账户名:")
21     if username_check(username) == 1:
22         break
23     else:
24         continue
25 while True:
26     password_first = input("请输入您的密码: ")
27     password_second = input("请再次输入您的密码")
28     if password_first == password_second:
29         file_reg = open('user.txt', 'a+')
30         file_reg.write(username)
31         file_reg.write(' ')
32         file_reg.write(password_first)
33         file_reg.write('\n')
34         break
35     else:
36         print("您输入的两次密码有误,请重新输入")
37         continue
38 print("注册成功,请记住您的账号和密码\n账号:{name}\n密码:{pwd}".format
39       (name=username,pwd=password_first))

猜你喜欢

转载自www.cnblogs.com/xiaolinzhu/p/10369643.html