程序需求:
輸入用戶名,密碼
認證成功顯示歡迎信息
輸入錯誤三次后鎖定用戶
流程圖:
好像畫的不咋地

查看代碼:
#!/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()方法以上這篇python密碼錯誤三次鎖定(實例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答