需求:
- 写一个用户登录窗口
- 验证输入的用户名和密码,若正确打印欢迎信息,输入错误三次则加入锁定名单。
- 锁定名单要持久化存储
1 # *-* coding:utf-8 *-* 2 # Auth: wangxz 3 import pickle 4 5 print("-------the login---------") 6 _username = "wangxz" 7 _passwd = 123456 8 9 def username_is_lock(name):10 with open(".\login.text", "rb") as fobj:11 name_list = []12 try:13 name_list.append(pickle.load(fobj))14 except EOFError: # 这个异常没有任何影响15 return None16 try:17 name_list.index(name)18 except Exception as e:19 return False # 没有在被锁定的文件中找到用户20 else:21 return True # 在被锁定的文件中找到用户22 23 24 25 RESUBMIT_COUNT = 0 # 定义一个常量26 while True:27 if RESUBMIT_COUNT <= 3:28 username = input("username: ")29 if not username_is_lock(username): # 没有找到文件30 passwd = input("password: ")31 if _username == username and _passwd == passwd: # 登录成功32 print("The login successfully!")33 break34 else:35 RESUBMIT_COUNT += 136 if RESUBMIT_COUNT == 3: # 已经输入三次,账户锁定37 print("The Warning! Your account has been locked")38 with open(".\login.text", "wb") as fd: # 写入文档39 pickle.dump(username, fd)40 break41 else:42 print("Please input your account and passwd again!")43 else: # 若已经在锁定文档中找到文件,则打印信息直接退出44 print("Your account has benn locked.\r\nPlease connection the wxz ")45 break
代码执行过程中有点问题:
在序列号存储时,不是追加写入,这次的写入会把上次的写入覆盖掉!有待研究!