练习题2 —— 模拟登陆: 用户输入帐号密码进行登陆 用户信息保存在文件内 用户密码输入错误三次后锁定用户,下次再登录,检测到是这个用户也登录不了




# f = open("info","r+",encoding='utf-8')
# info = {}
# i = 0
# while True:
#
#     account = input("请输入你的账号:")
#     if account in info:
#         i += 1
#         code = input("请输入你的密码:")
#         if code != info[account]:
#             if i == 3:
#                 print("账号已被锁定")
#                 del info[account]
#                 break
#     else:
#         code = input("设置密码:")
#         info[account] = code
#         break
# f.write(str(info))
# print(f.read())

user_name = 'alex'
password = 123
user_info = {
     }
count = 0
f = open('saa.txt.new.new.new', 'r+', encoding='utf-8')
file = f.read()
f.close()
print('请登陆'.center(30, '*'))
while count < 3:
    name = input('请输入用户名 : ')
    key = input('请输入密码 : ')
    key = int(key)
    if name in file:
        print('你的用户被锁定了')
        break
    elif name not in file:
        if name == user_name and key == password:
            print('登陆成功'.center(30, '-'))
            f2 = open('saz.txt', 'w', encoding='utf-8')
            user_info[user_name] = password
            f2.write(str(user_info))
            f2.close()
            break
        else:
            print('用户名或密码错误,请再试%s次' % (2-count))
        count += 1
        if count == 3:
            print('你的账户已被锁定'.center(30, '*'))
            f = open('saa.txt.new.new.new', 'r+', encoding='utf-8')
            file = f.write(name)
            f.close()


你可能感兴趣的:(python)