一、项目需求
1、根据用户名和密码,验证用户是否可登陆
2、允许一次执行可验证三次
3、当用户名输错三次后,该用户名锁定,永久不可登陆
二、代码如下
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 import operator 4 n = 0 5 username="user123" 6 password="123.abc" 7 count_dict={} 8 with open("lock",mode="r+",encoding="utf8") as f_read,open("lock_tmp",mode="r+",encoding="utf8") as f_write: 9 while n<3: 10 name=input("please input username:").strip() 11 pwd=input("please input passwd:").strip() 12 for line in f_read: 13 if name in line: 14 print("当前用户已经被锁定,请联系管理员!!") 15 exit() 16 if name == username and pwd == password: 17 print("欢迎%s登录"%username) 18 exit() 19 else: 20 print("用户名或密码错误,请重新输入!") 21 f_write.write(name+"\n") 22 n+=1 23 24 for line in f_write.readlines(): 25 line = line.strip() 26 count = count_dict.setdefault(line,0) 27 count+=1 28 count_dict[line]=count 29 for k in count_dict.keys(): 30 print(k) 31 print(count_dict[k]) 32 if count_dict[k] >= 3: 33 f_read.write(k+"\n")
时间: 2024-11-06 11:48:39