190327 Python登录接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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()