Day1 job: Write login interface, day1 job writing Interface
Job 1: Compile the login interface
1. Enter the user name and password
2. The welcome information is displayed after the authentication is successful.
3. Lock after three wrong attempts
Idea: the requirement is to write the login interface, so there must be a module for storing user information; after three locks, there must be a module for storing Locked User information; we know, there are two ways to save user information: database storage and file storage. Now we only learn how to save files. Therefore, there must be two files, one is the user information file, one is to lock the user information file.
To read information from the file, add information, and modify information.
Readme:
(1) enter the user name;
(2) Use the user name to lock the file and verify whether the file is locked. If the user is locked, contact the administrator;
(3) If the user file is not locked to verify whether the user exists, the user is allowed to enter the password, and the welcome information is displayed after the user is successfully entered; if the user fails to be entered three times, the user is locked;
(4) If the user is not in the user file, the system prompts that the user is not registered and needs to be registered. Write the registration module to let the user register. After registration, add the user to the user list and display the welcome information, users are not allowed to enter the information and are automatically linked to the logon status.
The flowchart is as follows:
Active = None # program execution identifier def verification (username): "verify whether the user is locked" locked_users = [] with open ("locked_file") as locked_f: for line in locked_f: locked_user, locked_pwd = line. split (":") locked_users.append (locked_user) print (locked_users) if username in locked_users: print ("sorry, your user name has been locked. Please contact the administrator! ") Else: active = True return activedef is_registered (username):" verifies whether a user is registered. After registration, the user is logged on, if the account is not registered, the user can re-enter the account or register "" users = {} with open ("active_file", "r +") as f: for active_line in f: user, pwd = active_line.split (":") users [user] = pwd if username in users. keys (): test_num = 0 while test_num <3: user_pwd = input ("enter your password:") if user_pwd = users [username]: print ("welcome back, have a good time! ") Return False else: test_num + = 1 else: # The user enters three or more times to lock the user, while... else... method print ("Sorry, you have entered too many times and your user has been locked. Please contact the administrator! ") Mes =" \ n "+ username +": "+ users [username] with open (" locked_file "," a ") as f1: f1.write (mes) return False else: print ("the user name you entered does not exist. Please follow the prompts below to select it! ") Print (" register: Enter 1 \ n login: enter 2 ") num = input (" Enter your choice: ") if num =" 1 ": register_name = input ("Enter the user name you want to register:") while True: register_pwd = input ("enter your password :") register_pwd2 = input ("enter your password again:") if register_pwd = register_pwd2: break else: print ("Your password is incorrect. Please enter it again ") message = "\ n" + register_name + ":" + register_pwd print ("Thank for your registing, have a good time! ") With open (" active_file "," a ") as f_obj: f_obj.write (message) return False elif num =" 2 ": user_name = input ("Enter your username again:") verification (user_name) if _ name _ = "_ main __": username = input ("Enter your username:") active = verification (username) while active: active = is_registered (username)
The code above implements the verification and lock functions and the registration function for new users. However, there is a defect that the user is not deleted from the current file when the user is locked, it is very easy to add information to the file to lock user information, but it is not very easy to delete a piece of information from the file. You cannot do anything as you like in the list. Only conversions can be performed first, and line breaks and so on are disgusting during file operations in the list. The above code will perform the following conversions. Learning is a tedious task, but it should also be done perfectly. The following will refer to the code written by others, learn how others can delete unnecessary information in files, and then improve the above Code.