Requirements: The user entered the user name and password error three times, the user is locked, do not let it log in.
def match (NAME,PWD): ' Match user input information, make judgment ' Falg = True while Falg:with open ("Test", mode= "R", encoding= "UTF8 ") as F_read, open (" Test2 ", mode=" R ", encoding=" UTF8 ") as F1_read:fread = F_read.readlines () F1read = F1_read.readlines () for lines in F1read: #循环黑名单里的内容 if name = = lines: #判断是否在黑名单里, Lock Exit Falg = false# exit while loop print ("This user has been locked!") If Falg ==false:break #退出黑名单循环 for line in Fread: #循环用户信息库, make a judgment if the name in Li NE and pwd in line:verdict = true# input is correct for true, exit loop break else: Verdict = false# input error is false, jump out of this loop continue if verdict = = True: #因为用户信息库里有很多用户信息, So, to be taken out to determine print ("Welcome%s"% (name)) Falg = False if verdict = = False: Print ("You input the userName or password error ") decide (name) #当输入错误时, call decide function Falg = Falsedef Decide (name):" Write user input wrong user name, reach a certain number of times, write the user to the blacklist ' Count = 0# counter, used to record the number of user input errors with open ("Test1", mode= "A +", encoding= "UTF8") as F_add: F_add.write ("\ n" +name) #往文本写错误用户的信息 f_add.seek (0) #把光标移到最开始位置 Fadd = F_add.readlines () #获取输入错误用户的信息 For I in range (len (fadd)): #循环列表fadd fadd[i] = fadd[i].replace (' \ n ', ') #去掉每行中的 "\ n" for the following pair of In Fadd: #此时的fadd里已没有多余的空格, easy to compare if Name = = line: #当有重复的用户名, accumulate count + = 1 #匹配后自加1 if c Ount > 2: #当输入3次时, just call black function black (name) def Black (name): "' blacklist, enter the number of times the user is locked in the blacklist ' with open (" tes T2 ", mode=" A + ", encoding=" UTF8 ") as F_add:f_add.seek (0) #移动光标至最开始位置 Fadd = F_add.readlines () #获取黑名单信息 fo R line in Fadd: #循环黑名单, duplicate users will not be written to if name = = Line:break #有重复的信息退出, no longer written to the blacklist f_add.write ( "\ n" +name) #写入黑名单while TrUE: #用户交互模式 name = input ("Please input your username:"). Strip () if name = = "Q": #输入q退出 print ("byebye!") Break pwd = input ("Please input your password:"). Strip () match (name, PWD) #调用match函数, match input information
In the program, test is the user's repository:
Michael---111egon---222tom---333
Test1: The wrong user is counted to cache information for the wrong user.
Test2: A blacklist of user information that reaches a specified number of times for storage. The user on the list cannot complete the login operation.
Python Combat One