用户输入帐号密码进行登陆
用户信息保存在文件内
用户密码输入错误三次后锁定用户,下次再登录,检测到是这个用户也登录不了
#内容写入程序
# accounts = {
# "Alex":["abc123",0],
# "Jack":["abc123",1]
# }
# with open("accounts.txt",'w',encoding = "utf-8") as f:
# f.write(str(accounts))
#打开程序
with open("accounts.txt","r",encoding= "utf-8") as f:
accounts = eval(f.read())
count = 0
is_same_user = True
last_username = None
while count < 3:
username = input("username:").strip()
password = input("password:").strip()
if last_username is None:
last_username = username
if username != last_username:
is_same_user = False
if username in accounts:
if accounts[username][1] == 0:
if username in accounts and password == accounts[username][0]:
print("welcome")
break
else:
print("wrong username or password")
else:
print("该用户已锁定")
else:
print("该用户不存在")
count += 1
else:
print("too many attemps")
if is_same_user is True:
accounts[username][1] = 1
with open("accounts.txt","w",encoding="utf-8") as f:
f.write(str(accounts))
print(accounts)