2017-06-17
Requirements Description:
1. Enter your user name and password
2. Verify the password and username, and verify that the welcome message is displayed
3. Password error three times after locking the user
Ideas:
1.account.txt is the file that holds the user ID and password
2.lock.txt is the file that holds the locked user ID, which is empty by default
3. Determine if the user is in the blacklist lock.txt
4. Determine if the user password is correct, after three failures, add the user ID to the blacklist
Code implementation:
1 #-*-coding:utf-8-*-2Count = 0#counter to record the number of times a user has entered a password3lock = []#define an empty list4User_pass = []5Flag = 16Username = Raw_input ("Please enter your name:")7f = open ("Lock.txt","r+")#read the contents of the blacklist8Lock_list =F.readlines ()9 f.close ()Ten forLock_lineinchLock_list:#traversing the blacklist file contents into the lock list Oneline = Lock_line.strip ('\ n') A Lock.append (line) - ifUsernameinchLock#determine if the user name entered is in the blacklist - Print("your%s user is already in the blacklist"%(username)) theExit ()#Exit Program - Else: - whileCount < 3: -Count = Count + 1 +passwd = Raw_input ("Please enter your password:") -f = open ('Account.txt','R') +User_file = F.readlines ()#read the user name and password in the Account.txt A f.close () at forIinchUser_file: -User_pass =I.strip (). Split () - ifUsername = = User_pass[0] andpasswd = = User_pass[1]:#The account password is correct, prompt login is successful, exit loop - Print("Login Successful ...") -Flag =True - exit () in Else: - Continue to ifFlag isTrue: + Break - Else: the ifCount = = 3:#The user is appended to the blacklist if the password is incorrectly 3 times * Print("your account has been locked ...") $f = open ("Lock.txt","a")Panax NotoginsengF.write ('%s\n'%username) - f.close () the Break
Pending optimization:
1. Does not determine the user does not exist in the Account.txt file the situation
2. Three failed to append to the blacklist of the user, the password is still able to log in correctly
Python 3-Time login Lock