1 #!Author:John 2 # _*_ coding: utf-8 _*_ 3 #編寫登陸接口 4 #輸入用戶名密碼 5 #認證成功後顯示歡迎信息 6 #輸錯三次後鎖定 7 import sys, os, getpass 8 9 10 limit = 3 11 count = 0 12 account_file = "account.txt" 13 lock_file = "locked.txt" 14 15 while count < limit: 16 username = input("Please input username:") 17 # 打開文件後第一步輸入用戶名,打開lock文件,檢查改用戶是否已經被鎖定 18 19 f1 = open(lock_file,'r') 20 # r後面不能加b,加b是以bytes類型打開,輸入的用戶名、密碼是字符串str和bytes不匹配 21 for line in f1.readlines(): 22 if username == line.strip(): 23 sys.exit("User %s has been locked!"%username) 24 f1.close() 25 password = input("Please input password:") 26 27 f = open(account_file, 'r') 28 match_flag = False 29 for line in f.readlines(): 30 user,pwd=line.strip().split() # 這裏的知識點須要掌握 31 if user==username and pwd == password: 32 print(username,"has match successfully!") 33 match_flag=True 34 break 35 f.close() 36 if match_flag==False: 37 print("Username or password is incorrect!") 38 count+=1 39 else: 40 print("Welcome login Python Learning system!") 41 break 42 else: 43 print("Your account has been locked!") 44 f1=open(lock_file,"a") 45 f1.write(username+'\n') 46 f1.close()