需求:
- 写一个用户登录窗口
- 验证输入的用户名和密码,若正确打印欢迎信息,输入错误三次则加入锁定名单。
- 锁定名单要持久化存储
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 None 16 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 break 34 else: 35 RESUBMIT_COUNT += 1 36 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 break 41 else: 42 print("Please input your account and passwd again!") 43 else: # 若已经在锁定文档中找到文件,则打印信息直接退出 44 print("Your account has benn locked. Please connection the wxz ") 45 break
代码执行过程中有点问题:
在序列号存储时,不是追加写入,这次的写入会把上次的写入覆盖掉!有待研究!