Requirements:
• Enter user name password
• The welcome message is displayed after successful authentication
• Three-time error after locking
1 #!/usr/bin/env Python 2 #-*-coding:utf-8-*-3 4 "" 5 User Login feature instance 6 1, get all user information in DB file 7 2, compare user information 8 If the user name does not exist, exit 9 directly If the user name has a 10 detection password, if the password exists, then the login is successful, and the number of login errors reset to 0, modify the memory in the dictionary logged in the number of login errors. (and write to file) 11 If the password does not exist 12 change the number of login errors logged in the In-memory dictionary 13 14 write the latest login errors and corresponding user information to the file 15 16 "" "17 18 19 # Read user information to U Ser_info_list read = file (' db ', ' r ') user_info_list = Read.readlines () read.close () 23 24 # Processing user_info_list for later use User name and password comparison 25 # processed results: user_info_dict = {' Alex ': {' pwd ': ' 112233 ', ' Times ': 0}, ' Eric ': {' pwd ': ' 111111 ', ' Times ': 0}}26 user _info_dict = {}27 for item in user_info_list:28 user_info = item.split (' | ') [User_info_dict[user_info[0]] = {' pwd ': user_info[1], ' Times ': Int (User_info[2].strip ())}30 and true:32 use Rname = raw_input (' Please enter user name: ') 33 # Username does not exist if username not in User_info_dict.keys (): print ' username does not exist, please re-enter! ' 36 # Username exists else:38 # count the number of logins if user_info_dict[username][' Times ' > 3:40 print ' account has been frozen, please contact the administrator! ' break42 else:43 pwd = raw_input (' Please enter password: ') if user_info_dict[username][' P WD '] = = pwd:45 # If the login is successful, reset the login to 046 user_info_dict[username][' times ' = 047 print ' Congratulations on%s, login successful '% username48 else:49 # If the login fails, add 150 login times user_info_dict[ Username][' times ' + = 151 52 # Write the latest login to file Temp_list = []54 for key, value in User_ Info_dict.items (): temp = "%s|%s|%d"% (key, value[' pwd '), value[' Times ']) temp_list.a Ppend (temp) temp_str = "\ n". Join (temp_list) w_obj = file (' db ', ' W '), W_obj.write ( TEMP_STR) W_obj.flush () W_obj.close ()
Python Case-User Login