练习题2 —— 模拟登陆:
用户输入帐号密码进行登陆
用户信息加注册时间保存在文件内
用户密码输入错误三次后锁定用户,下次再登录,检测到是这个用户也登录不了
储存账号的信息文件,涉及到文件名、和格式:
db-login.txt
dadada1|dadada1|2019-06-10 15:50:50
dadada2|dadada2|2019-06-10 16:02:03
dadada3|dadada3|2019-06-10 16:02:14
dadada|dadada|2019-06-11 18:29:03
dadada4|dadada4|2019-06-11 18:30:32
dadada5|dadada5|2019-06-11 18:34:43
dadada6|dadada6|2019-06-11 18:36:08
dadada100|dadada100|2019-06-11 18:43:40
dadada7|dadada7|2019-06-11 18:48:02
dadada8|dadada8|2019-06-11 19:16:50

db-ban.txt

1 dadada3|3 

代码:

  1 # @Time     :2019/6/7 23:01
  2 # -*- encoding:utf-8 -*-
  3 
  4 from _datetime import datetime
  5 
  6 def register():
  7     print("register".center(50,'*'))
  8     user_list = []
  9     # 把db信息读取到列表中,检测注册的账号是否已经存在
 10     with open('db-login.txt', mode='r+', encoding='utf-8') as f:
 11         for line in f:
 12             # print(line,type(line)) #asdd|asdd|2019-06-10 14:06:41
 13             user_list.append(line.split('|')[0])  # 存放db中的账号,检测注册账号是否重复
 14 
 15         username = input("username : ")
 16         pwd = input("pwd:")
 17         ctime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
 18         if username in user_list:
 19             print("账号已存在,请重新注册")
 20             return register() #调用该函数,重新注册  ***出了问题,如何调用自己继续注册?
 21         else:
 22             line = '%s|%s|%s\n' % (username, pwd, ctime)
 23             f.seek(0, 2)
 24             f.write(line)
 25             return True
 26 
 27 
 28 
 29 def login():
 30     sign = True #登录成功时,sign为False,否则将一直登录
 31     count = 0  # 计次,控制账号是否锁定
 32     while sign:
 33         db_login = {}#允许登录的信息
 34         db_ban = {} #被锁定账号的信息
 35         #把db-login.txt中的信息 存到 db_login字典中
 36         with open('db-login.txt',mode='r',encoding='utf-8') as f :
 37             for line in f :
 38                 line = line.split('|')
 39                 k,v =line[0], line[1]
 40                 db_login.setdefault(k,v)
 41 
 42         #把db_ban.txt中的信息 存到 db_ban字典中
 43         with open('db_ban.txt',mode='r',encoding='utf-8') as f :
 44             for line in f:
 45                 line = line.split('|')
 46                 db_ban.setdefault(line[0],line[1])
 47 
 48         print("login".center(50,'*'))
 49         username = input("username : ")
 50         pwd = input("pwd:")
 51 
 52         '''
 53         输入的username有以下几种情况:
 54             1.账号已锁定,无法登录
 55             2.count 等于 3  账号讲被锁定
 56             3.输入的账号不存在db_login字典中
 57             4.输入的账号存在db_login字典中:
 58                 1).账号密码正确 ==》登陆成功
 59                 1).账号密码不正确 ==》登陆失败 count+=1
 60         '''
 61         if db_ban.__contains__(username) :
 62             print("账号已锁定,无法登录")
 63 
 64         elif count == 2:
 65             print("错了3次,账号:%s 已经被锁定"%username)
 66             db_ban.setdefault(username,pwd)
 67             for k ,v in db_ban.items():
 68                 with open('db_ban.txt',mode='r+',encoding='utf-8') as f:
 69                     line = "%s|%s "%(k,v)
 70                     f.seek(0,2)
 71                     f.write(line)
 72 
 73         elif not db_login.__contains__(username):
 74             print("账号不存在")
 75 
 76         else:
 77             for k ,v in db_login.items():
 78                 if username == k and pwd == v :
 79 
 80                     sign = False
 81 
 82 
 83             if not sign :
 84                 print("登录成功")
 85 
 86             else:
 87                 print("账号密码不匹配")
 88                 count += 1
 89                 print("三次错误登录,账号讲锁定")
 90                 print("这是第%d登录" % count)
 91 
 92 
 93 def run():
 94     while True:
 95         print("注册输1\n登陆输2\n退出输q")
 96         choice = input(">>> ")
 97         if choice == '1':
 98             if register() :
 99                 print("注册成功")
100             else:
101                 print(" 注册失败")
102         elif choice == '2':
103             login()
104         elif choice == 'q':
105             exit()
106         else:
107             print("Wrong inputing")
108 
109 if __name__ == '__main__':
110     run()

 

posted on 2019-07-05 17:58  嗯哈哈  阅读(386)  评论(0编辑  收藏  举报