思路一:
# coding:utf-8 """ 登录接口: -输入用户名和密码 -认证成功后显示欢迎信息 -输错三次后锁定 """ import sys,os i = 0 while i < 3: name = raw_input('请输入用户名:') with open('D:/account_lock.txt','r+') as f: lock_list = f.readlines() for lock_user in lock_list: lock_user = lock_user.strip(' ') if name == lock_user: sys.exit('用户 %s 已被锁定' % name) with open('D:/account.txt','r') as f2: user_list = f2.readlines() for user in user_list: (user,password) = user.strip(' ').split() if name == user: j = 0 while j < 3: pwd = raw_input('请输入密码:') if pwd == password: sys.exit('用户 %s 登录成功' % name) else: if j != 2: print '密码错误,还有 %d 次机会请重新输入!' % (2 - j) j +=1 else: f.write(name + ' ') sys.exit('密码错误,用户 %s 达到最大登录次数,将被锁定并退出' % name) else: pass else: if i != 2: print '用户 %s 不存在,还有 %d 次机会请重新输入' % (name,(2 - i)) i +=1 else: f.write(name + ' ') sys.exit('用户 %s 不存在且达到最大登录次数,将被锁定并退出' % name)
思路二:
# coding:utf-8 """ 登录接口: -输入用户名和密码 -认证成功后显示欢迎信息 -输错三次后锁定 """ import sys i = 0 account_file = 'account.txt' lock_file = 'account_lock.txt' while i < 3: username = raw_input('Please input username:') lock_check = file(lock_file) for line in lock_check.readlines(): if username == line.strip(): sys.exit('User %s has been locked!' % username) password = raw_input('Please input password:') with open(account_file,'rb') as f: match_flag = False for line in f.readlines(): user,pwd = line.strip().split() if username == user and password == pwd: print username,'Match successful!' match_flag = True break if match_flag == False: print 'Username or password is wrong!' i += 1 else: print 'Welcome login doujiao system!' break else: print 'User %s has been locked!' % username with open(lock_file,'rb+') as f: f.write(username + ' ')