python密碼錯誤三次鎖定,python密碼錯誤鎖定
程式需求:
流程圖:
好像畫的不咋地
查看代碼:
#!/usr/bin/env python# _*_ coding:utf-8 _*_# File_type:一個登入介面# Author:smelondimport osusername = "smelond"#使用者名稱password = "qweqwe"#密碼counter = 0#計數器#讀取黑名單file = os.path.exists("./user.txt")#檢查目前的目錄是否有user.txt這個檔案,如果有者輸出True賦給fileif file == True:#判斷是否有user.txt這個檔案 blacklist_file = open("user.txt", "r").read()#open()開啟檔案,並且用read()讀取檔案,然後賦給blacklist_file if blacklist_file == username:#檢查檔案裡面的內容是否和我們的使用者名稱相等 print("Username lock. Please contact the administrator to remove the restrictions!!!")#輸出錯誤提示 exit()#退出程式#登入介面for i in range(3): counter += 1#對每次登入進行計數 input_user = input("Please input username: ") input_pass = input("Please input password: ") if input_user == username and input_pass == password: print("Welcome login...") break else: print("ERROR Incorrect username or password!!!") continue#寫入黑名單if counter == 3:#判斷我是否輸入錯誤三次 print("The user name has been disabled")#提示資訊 blacklist_user = open("user.txt", "a")#以追加模式開啟 (從 EOF 開始, 必要時建立新檔案) blacklist_user.write("%s" % username)#將使用者名稱寫入黑名單 blacklist_user.close()#使用open後一定要記得調用檔案對象的close()方法代碼