Day1 Job 1: Write the login interface and day1 job writing Interface

Source: Internet
Author: User

Day1 Job 1: Write the login interface and 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

Readme:

(1) prompt the user to enter the user name;

(2) Verify the user name to see if it is locked;

(3) locked or not: the user is locked and the user is locked. Contact the Administrator. If the user is not locked, check whether the user is registered;

(4) registration or not: if the user is not registered, the user is prompted. This user name is not yet registered and needs to be registered. If the user is registered, the user is asked to enter the password;

(5) enter the password. If the password is correct, the user is welcomed. If the password is incorrect, the user is prompted to enter the password. The password will be locked after three attempts.

(6) The Locked User is removed from the Login User File and added to the locked user file.

The flowchart is as follows:

 

Def islocked (username): # verify whether the user is locked. If the user is locked, print the lock. Otherwise, execute the logon module with open ("lock_user", "r") as f: # Open the file and verify users = [] # define an empty list for storing the user name lines = f. readlines () # because the object information is read, you need to put the user information in a list to easily operate on for line in lines: users. append (line. strip (). split (":") [0]) # generate a list containing all user names. if username in users: # verify that the user name entered is in the System user list, if yes, the system prompts that the user has locked print ("your username % s has been locked. Please contact the Administrator" % username) exit () else: # If it does not exist, verify that the isregister (username) de is registered. F isregister (username): # verify whether the user is registered with open ("user_list", 'R') as f_object: # open the file, generate the user list register_users = [] user_lines = f_object.readlines () for user_line in user_lines: register_users.append (user_line.strip (). split (":") [0]) if username in register_users: # verify whether the user is registered. if the user is registered, the pass else is skipped: # if the user is not registered, the system prompts that the user is not registered, and exit print ("Sorry, % s has not been registered, please register and log on" % username) exit () def userpassword (): # ask the user to enter the user password, and return to the calling function passw. Ord = input ("Please input your password:") return passworddef login (username): # user login module I = 0 islocked (username) # verify whether the user has locked registration with open ("user_list", "r") as f1: dic ={}# define an empty dictionary and generate a user name, password pairing dictionary user_lines = f1.readlines () for user_line in user_lines: # generate username-password dictionary (user, user_password) = user_line.strip (). split (":") dic [user] = user_password while I <3: # The user has three chances to enter the password to verify password = userpassword () # Call the function to allow the user to enter Password if dic [username] = password: # The password entered by the user is compared with the password stored in the system. if the password is consistent, log out of the logon page print ("Hello % s, welcome to back. "% username) break else: # if the user fails to receive three chances, the user will be locked if I! = 2: print ("sorry, the password you entered is incorrect. You still have % s chance" % (2-i) I + = 1 else: print ("sorry, you have entered too many times and your user name has been locked. Contact the Administrator. ") add_locked_user (username) # The user name is deleted from the file user_list, add to the lock file lock_user breakdef add_locked_user (username): # Too many attempts to lock the entered password username with open ("user_list", "r") as file: # open the file, and find the location of the user name to be locked. lines = file. readlines () # Read files row by row to generate a list of users = [] # define an empty list and locate the username location for line in lines: users. append (line. strip (). split (":") [0]) num = users. index (username) # locate the locked account name index in the list with open ("lock_user", "a") as f: # Add the user name to the locked file f. write ("% s \ n" % lines [num]) with open ("user_list", "w") as f_object: # Open the file del lines [num] In read-only mode # Delete the user's username f_object.writelines (lines) # Read the remaining users to the login file line by line (username = input ("Please input your username :"))

 

To run the code above, you need to create two files in the same directory as lock_user and user_list. The list, dictionary, and other functions are used. list and dictionary functions are the most commonly used, use the empty dictionary and empty list functions frequently, writelines () and readlines (). The code above can be used to determine whether the user's user name is in the locked list, whether the user is in the logon list, and whether the user can be locked for three times.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.