一、代码需求:
编写一个登录接口程序:
1、输入用户名、密码,认证成功后显示登录欢迎信息。
2、输错三次后,用户名锁定,再次登陆后依然锁定。
3、用户和黑名单信息从文件中读取,不断更新。
二、程序代码:
用户信息存在一个名为 “UserInformation.txt” 的文件里。
1 VisonWong abc123 2 VisonWong1 abc456 3 VisonWong2 abc789
黑名单信息存在一个名为 “BlackList.txt” 的文件里。
1 vison 2 wong 3 vionwong4
两个txt文件都在程序同级目录下。
第一版代码:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 #定义用户信息列表 6 User_Info = [] 7 #定义被锁用户列表 8 Black_Info = [] 9 #定义用户输入列表 10 User_Info_Input = [0,0] 11 12 #读取用户信息文件 13 _User_Info = open('UserInformation.txt') 14 for line in _User_Info.readlines(): 15 User_Info.append(line.split( )) 16 #测试代码用,打印已存在的用户信息 17 print("User_Info:{}".format(User_Info)) 18 _User_Info.close() 19 20 #读取被锁用户信息文件 21 _black_info = open('BlackList.txt') 22 B_read = _black_info.read() 23 Black_Info = B_read.split( ) 24 #测试代码用,打印已存在的黑名单信息 25 print("Black_Info:{}".format(Black_Info)) 26 _black_info.close() 27 28 count = 0 29 while count < 3: 30 UserName = input('Please input Username') 31 if UserName in Black_Info: 32 print('Sorry, your Username {} has already locked!'.format(UserName)) 33 count += 1 34 else: 35 User_Info_Input[0] = UserName 36 PassWord = input('Please input Password') 37 User_Info_Input[1] = PassWord 38 if User_Info_Input in User_Info: 39 print('Welcome to login in !') 40 break 41 else: 42 print('Please try again !') 43 count += 1 44 else: 45 print('Sorry, your Username {} is locked!'.format(UserName)) 46 _black_info = open('BlackList.txt','a') 47 #判断用户信息是否已存在黑名单 48 if UserName not in Black_Info: 49 _black_info.write(UserName) 50 _black_info.write(' ') 51 _black_info.close()
输出结果:
1、展示正常用户可登录,黑名单用户被锁无法登录功能。
1 E:PythonPythonExercisingRegistervenvScriptspython.exe E:/Python/PythonExercising/Register/Register.py 2 User_Info:[['VisonWong', 'abc123'], ['VisonWong1', 'abc456'], ['VisonWong2', 'abc789']] 3 Black_Info:['vison', 'wong', 'vionwong4'] 4 Please input Username vison 5 Sorry, your Username vison has already locked! 6 Please input Username VisonWong 7 Please input Password abc123 8 Welcome to login in ! 9 10 Process finished with exit code 0
2、展示同一用户输错三次密码后,即被锁定。
1 1 E:PythonPythonExercisingRegistervenvScriptspython.exe E:/Python/PythonExercising/Register/Register.py 2 2 User_Info:[['VisonWong', 'abc123'], ['VisonWong1', 'abc456'], ['VisonWong2', 'abc789']] 3 3 Black_Info:['vison', 'wong', 'vionwong4'] 4 4 Please input Username vison 5 5 Sorry, your Username vison has already locked! 6 6 Please input Username Visonwong 7 7 Please input Password abc123 8 8 Please try again ! 9 9 Please input Username Visonwong 10 10 Please input Password abc123 11 11 Please try again ! 12 12 Sorry, your Username Visonwong is locked! 13 13 14 14 Process finished with exit code 0
第二版代码:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 6 ####记录可登录用户、密码信息文件user_information#### 7 # VisonWong abc123 8 # VisonWong1 abc456 9 # VisonWong2 abc789 10 ####记录被锁定的用户信息文件locked_information#### 11 # vison 12 # Alex 13 14 #设置登录次数计数器和用户名重复次数计数器 15 count = 1 #设置计数器,统计下面第一个while大循环的次数 赋值变量Count,初始值为1 16 Last_user = "" #针对上次登录的用户名进行记录,如果连续三次登录错误的用户名是一致的话才进行锁定 17 #赋值变量Last_user,初始值为空 18 Match_count = 1 #统计Last_user与上次登录的用户名匹配的次数,变量Match_count初始值为1 19 20 while count <= 3: 21 22 Locked = open("locked_information", "r") # 读取被锁定用户名信息文件 23 User_pwd = open("user_information", "r") # 读取可登录的用户、密码信息文件 24 25 User = input("Please enter the login username:") #提示用户输入登录用户名并赋值给User 26 Passwd = input("Please enter login password:") # 提示用户输入登录密码并赋值给Passwd 27 28 for Blacklist in Locked.readlines(): #for循环读取被锁定用户名,生成变量Blacklist 29 Blacklist = Blacklist.strip(" ") #对变量Blacklist进行去换行符操作 30 if User == Blacklist: #判断如果输入的用户名在Blacklist中,则提示用户被锁定并退出登录 31 print ("Login username ",User," is locked, login fails.") 32 exit() #退出程序 33 34 for i_f_m in User_pwd.readlines(): #for循环读取可登录用户、密码信息文件,赋值变量i_f_m 35 i_f_m = (i_f_m.strip(" ")).split() #对变量i_f_m进行先去换行符,然后以空格符分割为列表的操作 36 if User == i_f_m[0] and Passwd == i_f_m[1]: #对登录用户名和密码进行匹配,两者都匹配成功则显示恭喜登陆成功 37 print ("Congratulations to login successful!") 38 exit() #退出程序 39 else: 40 print("The username or password is wrong!") 41 if 3-count >0: 42 print("You can also try {} times.".format(3-count)) 43 else: 44 print("Sorry,you had already try 3 times.") 45 46 if Last_user == User: #if语句判断,本次登录的用户名跟上次登录的用户名是否匹配 47 Match_count +=1 #如果匹配成功,表示两次连续登录的用户名一致,Match_count计数加1 48 else: #如果匹配失败,Match_count计数恢复为初始值1 49 Match_count = 1 50 51 if Match_count == 3: #连续三次输入的用户名一致,把用户名加入到黑名单 52 Locked = open("locked_information", "a") #对文件locked_information追加写操作 53 Locked.write(User + " ") #把用户名写入到黑名单并执行下换行符 54 print("Sorry,The current user {} is locked.") 55 break #提示锁定用户并退出程序 56 57 count +=1 #循环结束,Count计数加1 58 Last_user = User #循环结束,Last_user重新赋值
输出结果:
1 1 Please enter the login username:VisonWong 2 2 Please enter login password:fsdafg 3 3 The username or password is wrong! 4 4 You can also try 2 times. 5 5 Please enter the login username:VisonWong 6 6 Please enter login password:fdsf 7 7 The username or password is wrong! 8 8 You can also try 1 times. 9 9 Please enter the login username:VisonWong 10 10 Please enter login password:dffg 11 11 The username or password is wrong! 12 12 Sorry,you had already try 3 times. 13 13 Sorry,The current user VisonWong is locked. 14 14 15 15 Process finished with exit code 0
总结提高:
1、明确用户被锁定条件,只有相同用户名输错三次密码后才被锁定。
2、打开文件后读取到的内容为字符串。
3、Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。
4、Python split() 通过指定分隔符对字符串进行切片,返回分割后的字符串列表。