首页 > 代码库 > Python3 模拟用户密码输入三次错误后锁定

Python3 模拟用户密码输入三次错误后锁定

#locklist.txt模拟锁定用户列表 格式如下
#user1
#user2

#userlist.txt模板注册用户列表 格式如下
# user1:password1
# user2:password2

#初始化输入次数
count = 0

while True:
    #获取用户输入
    username = input("Input your username:")
    password = input("password:")
    
    #判断输入用户是否在锁定用户列表
    lock_list = open("locklist.txt","r")
    while True:
        lock_list_line = lock_list.readline().split(":")
        if username == lock_list_line[0].replace("\n",""):
            print("用户被锁")
            exit()
        elif lock_list_line[0] == "":
            break
    lock_list.close()

    #判断用户密码是否正确
    user_list = open("userlist.txt","r")
    while True:
        user_list_line=user_list.readline().split(":")
        if username == user_list_line[0] and password == user_list_line[1].replace("\n",""):
            print("Welcom " + username)
            exit()
        elif username == user_list_line[0]:
            count +=1
            #同一用户密码错误三次将用户写入锁定列表
            if count == 3:
                lock_list = open("locklist.txt","a")
                lock_list.write(username+"\n")
                lock_list.close()
                exit("用户被锁")
            print("密码输入错误!")
            break
        elif user_list_line[0] == "":
            print("用户不存在!")
            break
    user_list.close()

 

Python3 模拟用户密码输入三次错误后锁定