1 #!/usr/bin/env python2 #Coding:utf-83 4 """5 AIM: User Login program based on File Store (3 login failures, lock users)6 7 need:8 A. User information file9 B. User inputTen One Logical: A A. Verifying the legal status of a user name - B. Verifying user lock-in conditions - c. Check password the D. Updating the number of logon failures - E. Writing files - - sum: + A. Update user lockout number do not directly manipulate the file, you can write to the memory, the program to write the file before exiting - B. Logical judgment Situation + """ A at #read file, get user information str -F1 = open ('DB','R') -Content =F1.read () - f1.close () - - #Convert user information from STR to list inUser_data_list = Content.split ('\ n') -User_detail_list = [] to + #Save user information as a dictionary to the list - forUser_infoinchuser_data_list: theUser_detail = User_info.split ('|') * User_detail_list.append ({ $ 'name': user_detail[0],Panax Notoginseng 'pwd': user_detail[1], - ' Times': user_detail[2] the }) + A Print(""" the Welcome to the old Boys education Group, please login first! + """) - $ #Main program exit flag bit $Flag =True - #number of Exits -Exit_flag = 3 the #Main Process - whileflag:Wuyiuser_name = input ("User name (quit [quit]):") the #Exit Judgment - ifUser_name! ='quit': Wu #Traverse user Information list - forIteminchuser_detail_list: About #determine if the user name exists $ ifUser_name = = item['name']: - #user exists to determine whether to lock - ifInt (item[' Times']) <Exit_flag: -User_pwd = input ("Password:") A #Verifying passwords + ifUser_pwd = = item['pwd']: the Print('Login Successful') - #Update Dictionary $item[' Times'] =0 the #update while loop flag bit theFlag =False the #Exit For Loop the Break - Else: in Print('Password Error') the #Update Dictionary theitem[' Times'] = Int (item[' Times']) + 1 About #Exit For Loop, continue to enter user name the Break the Else: the Print("User is locked") + Break - Else: the Print('user name does not exist')Bayi Else: theFlag =False the - #Finally, the user information is written to the file - #Working with Strings theResult_info ="' the forIteminchuser_detail_list: theItem_str ='%s|%s|%s'% (item['name'], item['pwd'], item[' Times']) the ifResult_info.strip () = ="': -Result_info + =Item_str the Else: theResult_info = Result_info +'\ n'+Item_str the 94 #Write File theF2 = open ('DB','W') the f2.write (result_info) theF2.close ()
Python script implements a file store-based user login program