1. 程式人生 > >Python3 模擬用戶密碼輸入三次錯誤後鎖定

Python3 模擬用戶密碼輸入三次錯誤後鎖定

[1] lac () pri split bre col 鎖定 註冊

#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 模擬用戶密碼輸入三次錯誤後鎖定