# 登陆认证程序
# 作业需求:
# 基础需求:
# 让用户输入用户名密码
# 认证成功后显示欢迎信息
# 输错三次后退出程序
#
# 升级需求:
# 可以支持多个用户登录(提示,通过列表存多个账户信息)
# 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示: 需把用户锁定的状态存到文件里)
f = open('login_system.txt')
all_info = [line.strip() for line in f.readlines()]
f.close()
# print(all_info)
for index,k in enumerate(all_info):
all_info[index] = eval(k)
# print(all_info)\
count = 0
exit_flag1 = False
while not exit_flag1:
exit_flag2 = False
exit_flag3 = False
choice = input('请选择(l(L):登陆,s(S):注册):')
if not choice:
print('\033[1;0m输入错误\033[0m')
elif choice == 's' or choice =='S':
user_info = {'username': ' ', 'password': ' ', 'count': 0}
isexit = False
while not exit_flag2:#判断用户名是否存在
username = input('用户名:')
if not username:
continue
if len(all_info) != 0:
for k in all_info:
if username == k['username']:
isexit = True
print("\033[1;0m用户名已存在\033[0m")
break
exit_flag2 = True
if not isexit:
password = input('密码:')
user_info["username"] = username
user_info["password"] = password
all_info.append(user_info)
with open('login_system.txt', 'a') as f:#写进文件
f.writelines(str(user_info)+'\n')
print('\033[1;0m注册成功\033[0m')
elif choice == 'l' or choice == 'L':
if len(all_info) == 0:#如果没有用户
print('请注册!')
exit_flag3 = True
while not exit_flag3:#判断用户名是否不存在
username_exit = False
username = input('用户名:')
for k in all_info:
if username == k['username']:#如果用户名存在
if k['count'] < 3:
k['count'] = 0#上次登录输错密码次数少于3,则清零
temp_password = k['password']
while True:
password = input('密码:')
if password == temp_password:#判断密码是否正确
print('\033[1;0m登陆成功!\033[0m')
break
else:
count += 1
k['count'] = count
# print(count)
# print(all_info)
with open('login_system.txt','w') as f:#将列表写进文件
for k1 in all_info:
f.writelines(str(k1)+"\n")
print('\033[1;0m密码错误!\033[0m')
if count == 3:
print('\033[1;0m密码输入错误已超3次!账号已被锁定!\033[0m')
k['count'] = 3
exit()
username_exit = True
exit_flag1 = True
exit_flag3 = True
break
else:
print('\033[1;0m账号已被锁定!\033[0m')
exit()
if not username_exit:
print('\033[1;0m用户名不存在\033[0m')
exit_flag3 =True
else:
print('\033[1;0m输入错误\033[0m')