first, the demand
Writing the Login interface
1. Enter the user name password
2. Display welcome message after successful authentication
3. Three times after the wrong lock
second, simple ideas
Login, three times password input error lock user
1. User Information file: Store user name and password
2. Blacklist file: Add three wrong users to this list, users who join this list will not be allowed to log in
Third, the concrete realization
The first step: in the blacklist to detect whether there is this user, if any, do not let the login
Second step: User name and password determination
The code is as follows:
#!/usr/bin/env Python3
Count = 0 #记录用户输入密码的次数flag = 1 #标志位lock = []user_pass = []username = input (' Please input your name: ') #读取黑名单的内容f = Ope N (' Black_mingdan ', ' r ') Lock_file = F.readlines () f.close () #将黑名单文件内容作为列表元素追加到列表中for i in lock_file:line = I.strip (' \ n ') Lock.append (line) #若输入的用户名在黑名单中, if you are given a message: The user has been locked out, please contact the administrator. If username in Lock:print (' User%s has Been locked.it is not ' Allow to login,please. ' Administrator ') Else: #输入的用户名不在黑名单中 prompts the user for password information while True:count + = 1 passwd = input ("Please input your password:") f = open (' user_information ', ' r ') User_file = F.readlines () f.close () for I in User_file: User_pass = I.strip (). Split () #判断输入的用户名 ==user_pass[0] and password ==user_pass[1], if equal, prompts the welcome message and exits the loop, if not equal #结束本次循环 if username = = User_pass[0] and passwd = = User_pass[1]: print (' Welcome user%s login! ') %USERNAME) flag = True Break Else: Continue #若flag为真, the user name and password input correctly jump out of the entire loop body, conversely, if the user entered a password error number is 3, give a hint message: The user has been locked #并将username追加到黑名单中 if FLA G is true:break else:if count = = 3:print (' User has Been try 3 times,have B Een Locked ') lock_file = open (' Black_mingdan ', ' a ') #lock_file. Write (' Have Been Locked user:% S\n '%username) lock_file.write ('%s\n '%username) lock_file.close () break
The results of the program execution are as follows:
[Root] python3 denglu.py Please input your name:zhangsanplease input your password:123welcome user Zhangsan login! [root]# python3 denglu.py Please input your name:zhangsanplease input your password:098please input your Password:678pleas E input your password:543user has Been try 3 times,have Been locked[root]# python3 denglu.py please input your Name:zhang Sanuser Zhangsan has Been locked.it is isn't allow to login,please contact Administrator. [root]# Cat Black_mingdan Zhangsan
Python loop and file operation to implement user password error three times lock user