简单的用户登录(文件处理)

 1 import hashlib
 2 count=3     #剩余输入次数
 3 
 4 #用户注册
 5 def reg(user,psd):
 6     if user==n_name:
 7         print('%s该用户已被注册,请更换!'%n_name)
 8     else:
 9         hash = hashlib.sha256()
10         hash.update(psd.encode('utf-8'))
11         m_psd = hash.hexdigest()
12         with open('abc.txt', 'a+', encoding='utf-8')as f:
13             f.write(user)
14             f.write(',')
15             f.write(m_psd)
16             f.write('\n')
17         return 'Register successful!'
18 
19 while count>0:
20     user=input('请输入用户名').strip()
21     psd=input('请输入密码').strip()
22     hash=hashlib.sha256()     #密码sha256加密
23     hash.update(psd.encode('utf-8'))
24     m_psd=hash.hexdigest()
25 
26 
27     info = {}
28     with open('abc.txt','r+',encoding='utf-8')as f1:   #打开数据文件
29         fr= f1.readlines()
30         for i in fr:
31             n_name=i.split(',')[0]    #后台用户名
32             n_psd=i.split(',')[1][0:-1]     #后台密码
33             info.setdefault(n_name,n_psd)
34 
35         if user in info:    #用户名匹配成功
36             ret=info.get(user)
37             if m_psd==ret:
38                 print('用户%s登录成功!欢迎进入本系统>>>>>'%user)
39                 break
40             else:
41                  count -= 1
42                  print('用户名密码输入错误,剩余%s次机会,请重试!' % count)
43         else:         #用户名匹配失败
44             register = input('您尚未注册,是否现在注册(Y/N)').strip()
45             if register.isalpha() and register.upper()=='Y':
46                 user = input('请输入用户名')
47                 psd = input('请输入密码')
48                 suf=reg(user,psd)
49                 print(suf)
50             else:
51                 print('不注册玩个锤子!')

猜你喜欢

转载自www.cnblogs.com/wen-kang/p/9279631.html
今日推荐