1、 userinfo.txt里保存的是用户名密码清单,设计一个程序,从键盘输入用户名和密码

userinfo.txt里保存的是用户名密码清单,设计一个程序,从键盘输入用户名和密码,与userinfo.txt里的信息比对,如一致则提示成功登录,如失败,则提示用户名或密码错误,重新输入。用户密码输入错误三次后锁定用户,下次再登录,检测到是这个用户也登录不了。

img

给个例子做参考

fd = open('userinfo.txt', 'r')
lines = fd.readlines()
fd.close()
user_pwd = {}
for line in lines:
    user = line.split(' ')[0].strip()
    pwd = line.split(' ')[1].strip()
    user_pwd[user] = {}
    user_pwd[user]['pwd'] = pwd
    user_pwd[user]['error_times'] = 0
print(user_pwd)
while True:
    [user, pwd] = input("输入账户和密码,用空格隔开:").strip().split(' ')
    if user in user_pwd.keys() and pwd == user_pwd[user]['pwd'] and user_pwd[user]['error_times']<3:
        print('成功登录')
    elif user in user_pwd.keys() and user_pwd[user]['error_times']>=3:
        print(f'{user}用户密码输入错误三次,已锁定用户')
    else:
        print('用户名或密码错误,重新输入')
        if user in user_pwd.keys():
            user_pwd[user]['error_times'] += 1

如有帮助,请采纳!