1、流程图
2、代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 #Author: Tony Chiu 4 #Blog:http://www.cnblogs.com/tonychiu 5 #Github:https://github.com/qiujichun 6 ''' 7 match.txt 8 user1:pass1 9 user2:pass2 10 user3:pass3 11 user4:pass4 12 locked.txt 13 u1 14 u2 15 u3 16 ''' 17 import sys 18 retry_count = 0 19 20 #指定账户、锁定信息路径 21 account_file = (r'match.txt') 22 locked_file = (r'locked.txt') 23 24 # 输入用户名 25 username = input("Insert your name:") 26 27 # 判断用户是否锁定 28 lockinfo = open(locked_file, "r") 29 for line in lockinfo.readlines(): 30 if username == line.strip(): 31 print("Your account %s is be locked"%username) 32 sys.exit() 33 lockinfo.close() 34 35 #匹配用户名和密码 36 for i in range(3): 37 retry_count = retry_count+1 38 password = input("Insert password:") 39 userinfo = open(account_file, "r") 40 for line in userinfo.readlines(): 41 User, Pass = line.strip().split(':') 42 if username == User and password == Pass: 43 print("Success!") 44 sys.exit() 45 else: 46 continue 47 userinfo.close() 48 49 #尝试密码超过3次将锁定账户 50 if retry_count == 3: 51 print("Too many attempts,%s will be locked...."%username) 52 lockinfo = open(locked_file, "a") 53 lockinfo.write('\n' + username) 54 lockinfo.close() 55 sys.exit()
3、测试
3.1输入已被锁定的用户
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Insert your name:u1 2 Your account u1 is be locked 3 4 Process finished with exit code 0
3.2输入正确信息
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Insert your name:user1 2 Insert password:pass1 3 Success! 4 5 Process finished with exit code 0
3.3输出错误密码3次
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Insert your name:user1 2 Insert password: 3 Insert password: 4 Insert password: 5 Too many attempts,user1 will be locked....