需求
1.输入用户名密码
2.认证成功后显示登录成功
3.密码输错三次后锁定
实现思路
1.读取保存在文件中的账号和密码
2.用户输入用户名
3.判断用户名是否在锁定文件内,被锁定则提示然后退出
4. 未被锁定则判断用户名是否存在
5.用户名存在,判断密码是否正确,输错三次则将用户名写入锁定文件
import getpass
with open("login", "r", encoding="utf-8") as fr:
data = fr.readline().strip()
dic = eval(data)
count = 0
while True:
uname = input("请输入用户名:")
with open("lock", "r", encoding="utf-8") as fr_lock:
data_l = fr_lock.readline().strip()
lock_list = data_l.split(" ")
if uname in lock_list:
print("该账户已经被锁定,无法登录!")
break
else:
# pwd = input("请输入密码:")
pwd = getpass.getpass("Input password:")
if uname in dic:
if pwd != dic[uname]:
print("用户名或密码错误!")
count += 1
if count == 3:
print("账户被锁定")
with open("lock", "a", encoding="utf-8") as fw:
fw.write(uname+" ")
fw.flush()
break
else:
print("登录成功")
break
else:
print("用户名不存在!")