要求
- 输入账号密码完成验证,验证通过后输出"登录成功"
- 可以登录不同的用户
- 同一账号输错三次锁定(附加功能,在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁)
- 扩展需求:在3的基础上,完成用户一旦锁定,无论程序是否关闭,都锁定5分钟
import time, os
user_pwd = {"egon":"root123", "mac":"13579", "tank":"root"}
user_info = {"egon":0, "mac":0, "tank":0}
def lock(name):
user_info[name] = "lock"
print("你的账号已被锁定")
with open('locked.txt', 'w', encoding='utf-8') as f:
f.write("lock "+name)
time.sleep(3)
user_info[name] = 1
with open('locked.txt', 'w', encoding='utf-8') as f:
f.write("None None")
print("账号解锁")
def locked():
if os.path.exists('locked.txt'):
with open('locked.txt', 'r', encoding='utf-8') as f:
all = f.read()
a = all.split()
flag = a[0]
name = a[1]
return flag, name
else:
print("文件不存在")
while True:
userName = "".join(input("请输入用户名:").split())
password = "".join(input("请输入用户密码:").split())
flag, name = locked()
if not(userName in user_pwd):
print("用户不存在")
elif (userName == name and flag == "lock"):
lock(userName)
elif (user_pwd.get(userName) != password):
user_info[userName] += 1
print("用户密码错误")
if (user_info.get(userName) >= 3):
lock(userName)
else:
print("登录成功")
break