Write a login interface instance

Source: Internet
Author: User
Requirements: Writing the login interface

1. Enter your user name and password

2. Display welcome message after successful authentication

3. Three times after the wrong lock

1) Writing ideas

Writing ideas refer to the flowchart in the GitHub link below

Job/day1_ Job _ Login interface flowchart. png

2) Concrete implementation

 1 #-*-Coding:utf-8-*-2 3 # author:chuixin Zeng 4 5 # The difference between a dictionary and a list: The list is sequential, indexed, and the entries in the dictionary are accessed with a key 6 7 # Initialize NULL dictionary 8 _user_d ICT = {} 9 # Initialize empty list after an empty list, the program can add elements to this list _user_list = []11 # initialization login does not exist the initial value of the user name is 012 _login_error_count = 013 14 # to make Start an unrestricted loop with while true:16 # Interactive input login username and password _user_naming = input ("Pls input your Username:") _user_pass      Word = input ("Pls Input your Password:") 19 # Read User login data file userpassword.txt20 _users_data = open (' UserPassword.txt ') 21 # Loop through the Open data, format the Open data, and then save the formatted data to the created _user_dict dictionary, user_data in _users_data:23 # using strip to remove U Ser_data the front and back spaces in the data row, _user_list = User_data.strip () 25 # will remove the data rows after the trailing spaces _user_list separated by commas and re-saved to _user_datas 2         6 _user_datas = _user_list.split (', ') 27 # The data in the No. 0 position in the data row (that is, the user name) is removed, and after the trailing space is stripped, a new variable is assigned _USER_NAME28 _user_name = _user_datas[0].strip () 29 # will _user_datas data (that is, password) in the 1th position in the row, and after the trailing space is removed, the new variable is assigned _USER_PASS30 _user _pass = _user_datas[1].sTrip () 31 # will _user_datas data in the 2nd position in the data row (that is, lock state, 0 for unlocked, 1 for lock), and after the trailing space is removed, assign the new variable _user_locked32 _user_locked = _user _datas[2].strip () 33 # _user_datas The data in the 3rd position in the data row (that is, the number of password entries), and after the trailing space is removed, the new variable _user_error_count34 # will be _user_data S[3] Data type cast to Int35 _user_error_count = Int (_user_datas[3].strip ()) 36 # Save the previously defined variable information in the dictionary, specifying the key name for the preceding value, which is the key: value pair Should be the relationship, the key name can be casual, it is recommended to consider legibility 37 # If the following key name line below the wavy lines, does not mean that there is a syntax error, but the editor thinks the spelling of English is not the correct English word, does not affect the script execution 38 # Follow the Python programming specification, after the comma and colon                                   Need to add a space _user_dict[_user_name] = {' name ': _user_name, ' password ': _user_pass,40 ' Locked ': _user_locked, ' errorcount ': _user_error_count}41 42 # Close Open UserPassword.txt file _users_data.close () 44     45 # Determine whether the user account has been locked, the user name entered in the previous interface and the dictionary saved user names to match 46 # match the previous interactive input username and dictionary userlocked key, if the user name corresponding to the key is 1, is locked, log out 47 If _user_naming in _user_dict.keys (): If _user_dict[_user_naming][' locked '] = = ' 1 ': Print ("The AC Count was Locked,pls conTact administrator! ") BREAK51 # matches the username, password, and user name in the dictionary entered previously, and if successful, outputs the login system and exits the _user_naming = = _user_dict[_us er_naming][' name '] and _user_password = = _user_dict[_user_naming][' password ']:53 print ("Welcome to system!") Break55 else:56 # If the user name is in the dictionary, but the password is incorrect, the number of errors in the dictionary for the user name entered earlier is increased by the _user_dict[_u ser_naming][' errorcount ' + = 158 # If the username is entered correctly, the number of password input errors is less than 3 times, giving the user the chance to try again if _user_dict[_user_na ming][' Errorcount ' < 3:60 print ("The password was Wrong,pls try Again,your has three chance!") 61 # Append write new data to UserPassword.txt, where the errorcount will increase with the number of failed attempts, three times is 2, that is 0,1,262 _write_data = O Pen (' UserPassword.txt ', ' w+ ') 63 # takes the value of the dictionary inside the _user_value, loops through the "for _user_value" in _user_ Dict.values (): 65 # Go to the empty list defined at the beginning of the script and write the data row by line _user_list = [_user_value[' name '], _user _value[' PassWord '], str (_user_value[' locked '), [_user_value[' Errorcount '])]68                     # Assign the data written to _user_list once again formatted to _users_list69 _users_list = ', '. Join (_user_list) 70                 # writes data from _users_list to UserPassword.txt and wraps at the end of each line of data _write_data.write (_users_list + ' \ n ') 72 # Close UserPassword.txt document _write_data.close () else:76 # If the password is incorrect More than 3 times, the output account is locked in three print ("You try Times,account locked") 78 # Output account is locked while the dictionary is locked The value of the key is set to 1, this place is changed to 1, the front to determine if the locked state is effective _user_dict[_user_naming][' locked '] = 180 # After the account is set to lock, The user's corresponding error password number is zeroed, I understand that the lock property is 1, the number of errors can be cleared 81 _user_dict[_user_naming][' errorcount '] = 082 _write _data = open (' UserPassword.txt ', ' w+ ')-_user_value in _user_dict.values (): _us Er_list = [_user_value['Name '], _user_value[' password '], str (_user_value[' locked ']), (_user_value[' Errorcou NT '])]86 _users_list = ', '. Join (_user_list) _write_data.write (_users_list + ' \ n ') ) _write_data.close () break90 else:93 # Output Information 94 pri If the user name does not exist NT ("Pls ensure input currect account or password,you can try three times!")  95 # Program at the beginning set the initialization does not exist the user name value is 0, here to self-increment _login_error_count + = 197 # If the number of user attempts does not exist more than 3 times, the interrupt program 98 if _login_error_count > 2:99 Break

3) GitHub notes

The address of the first day's note is:

Practice with the church

The address of the first day assignment is:

Homework

4) README.MD documentation

Job/readme_ login interface. MD

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.