Project Requirements Description: Require the user to enter a user name and password, after the successful authentication display welcome information, if the consecutive error three times to lock the user name.
Logical Flowchart:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4B/BB/wKiom1QxZy7zr_O7AAFW6UDf-Tw524.jpg "title=" 1.14_ User Login interface flowchart. png "alt=" wkiom1qxzy7zr_o7aafw6udf-tw524.jpg "/>
Implementation code:
#!/usr/bin/env pythonimport sysaccount_file = ' account.txt ' lock_file = ' lock.txt ' # put accounts in a listfh_account = open (account_file) account_list = fh_account.readlines () fh_account.close () # initialize retry count as 3 for every accountretry_count = {}for line in account_list: line = line.split () retry_count[line[0]] = 3while true: # put locked accounts in a list Fh_lock = open (Lock_file) lock_list = [] for i in fh_lock.readlines (): line = I.strip (' \ n ') lock_list.append (line) fh_ Lock.close () # handle the username and password empty issue username = raw_input (' username: '). Strip () if len ( username) == 0: print ' \033[31;1musername should not be empty !\033[0m ' continue password = raw_input (' password: '). Strip () if len (password) == 0: print ' \033[31; 1mpassword should not be empty !\033[0m ' continue # authentication part if username in lock_list: print "\033[31;1msorry, '%s ' is locked already ! \033[0m " % username continue If not retry_count.has_key (username): # inexistent account retry_count[username] = 3 for line in account_list: line = line.split () if username == line[0] and password == line[1]: # authentication pass retry_count[username] = 3 # reset retry times for this account sys.exit (' \033[32;1mwelcome %s login my system !\033[0m ' % username) else: # authentication failed print ' \033[ 31;1mwrong username or password !\033[0m ' retry_count[username] -= 1 if retry_count[ username] > 0: print ' \033[31;1myou have %d more chances !\033[0m ' % retry_count[username] else: fh = open (lock_file, ' a ') fh.write ('%s\n ' % username) fh.close () print "\033[31;1msorry, '%s ' is locked !\033[0m " % username
Using Python to implement the login interface, allowing to try three times