2020-6-29-Python3-day1作业
1 # -*- coding:utf-8 -*- 2 __author__ = 'admin' 3 ''' 4 locked.txt: 5 liuxiaoyu 6 xiaodong 7 tenglan 8 match.txt: 9 alex 123456 10 xiaoyu 6543210 11 wulongyuan 6667770 12 ''' 13 import sys 14 15 account_file = 'c:\python作业\day1\match.txt' 16 locked_file = 'c:\python作业\day1\locked.txt' 17 18 def deny_account(user_name): 19 print('您的用户 %s 已被锁定!' % user_name) 20 with open(locked_file, 'a') as deny_f: 21 deny_f.write('\n' + user_name) 22 23 def main(): 24 retry_count = 0 25 retry_limit = 3 26 while retry_count < retry_limit: 27 user_name = input('\033[32;1m请输入用户名:\033[0m') 28 #判断用户名是否为空 29 if len(user_name) == 0: 30 print('用户名不能为空,请重新输入!') 31 continue 32 #判断用户名是否被锁定 33 with open(locked_file, 'r') as lock_f: 34 for line in lock_f: 35 if len(line.strip()) == 0: 36 continue 37 if user_name == line.strip(): #strip()去掉两侧的空格和\n 38 sys.exit('\033[32;1m用户 %s 已经被锁定!\033[0m' % user_name) 39 #判断密码是否为空 40 while True: 41 password = input('\033[32;1m请输入密码:\033[0m') 42 if len(password) == 0: 43 print('密码不能为空,请重新输入!') 44 continue 45 else: 46 break 47 #判断用户名和密码是否相符 48 with open(account_file, 'r') as account_f: 49 flag = False 50 for line in account_f: 51 #跳过空行 52 if len(line.strip()) == 0: 53 continue 54 user, pawd = line.strip().split() 55 if user_name == user and password == pawd: 56 print('success!') 57 flag = True 58 break 59 #判断错误次数是否超过3次 60 if flag == False: 61 if retry_count < 2: 62 print('您的用户名或密码有误,请重新输入!') 63 retry_count += 1 64 continue 65 else: 66 print('欢迎用户%s登陆老男孩系统!' % user_name) 67 break 68 else: 69 deny_account(user_name) 70 71 if __name__ == '__main__': 72 main()