需求:编写登陆接口
- 用户输入帐号密码进行登陆
- 用户信息保存在文件内
- 用户密码输入错误三次后锁定用户
#待改进:
#username & password这里应该+输入判定 不能为空不能乱码之类的
#不漂亮
#不具备现实可操性
import sys
while 1:
count = 0
username = input("请输入你的用户名").strip()
black_list = []
with open('black', 'r+') as black:
# for el in black:
black_name_list = black.readlines()
for el in black_name_list:
el = el.strip()
black_list.append(el)
#print(black_list)
with open('userinfo.txt', 'r+') as u:
user_list = u.readlines()
dic = {}
list_k = []
list_v = []
for el in user_list:
name,pwd = el.split()
dic[name] = pwd
for k,v in dic.items():
list_k.append(k)
list_v.append(v)
if username in black_list:
print("账户被锁定,请联系游戏管理员.")
sys.exit()
else:
if username not in list_k:
print("用户不存在,请重新输入")
sys.exit()
while 1:
password = input("请输入你的密码吧~").strip()
if password == dic[username]:
print("登录成功")
break
elif count <= 3 and password != dic[username]:
print("重新输入,你还有%s机会" % (3 - count))
count += 1
elif count > 3:
with open('black','w') as black_new:
black_new.write(username + '\n')
print("账户被锁定,请联系游戏管理员")
sys.exit()
black.close()
u.close()