Python from entry to mastery (DAY3)

Source: Internet
Author: User
This is the third article in the series from getting started to mastering python. It mainly describes the whole process of writing a login interface exercise program for everyone, which is very meticulous, if you have any need, refer. Requirement: Write the login interface

Enter user name and password
Welcome information displayed after successful authentication
Lock after three wrong attempts

There are two types of scripts for this instance, which are slightly different, as shown below:

The account number file account.txt is as follows:

Sam 123

David 12
Kevin 123
Lin 12
Tailen 123
Jack 12

The default value of the locked file account_lock.txt is null.

1. Only identify and lock users in the account file. There are three error retry opportunities for users and passwords.

1. The flowchart is as follows:

The Code is as follows:

#! /Usr/bin/python27 # _ * _ coding: UTF-8 _ * _ import sys, OS, getpassos. system ('clear') I = 0 while I <3: # As long as the user login exception does not exceed 3 times, the name = raw_input ("Enter the User name :") lock_file = open('account_lock.txt ', 'r +') # After the user enters the user name, open the LOCK file to check whether the user has locked lock_list = lock_file.readlines () for lock_line in lock_list: # loop LOCK file lock_line = lock_line.strip ('\ n') # Remove the linefeed if name = lock_line: # exit sys directly if LOCK is enabled. exit ('user % s is locked and exit '% name) user_file = open('account.txt', 'R') # open the account file user_list = user_file.readlines () for user_line in user_list: # traverse account files (user, password) = user_line.strip ('\ n '). split () # obtain the account and password respectively if name = user: # if the user name matches normally j = 0 while j <3: # As long as the user password is abnormal no more than three times, passwd = getpass will be continuously cyclically. getpass ('Enter the password: ') # enter the hidden password if passwd = password: # The password is correct, and you are prompted to log on to print (' Welcome to log on to the management platform, USER % s' % name) sys. exit (0) # exit else normally: print ('user % s password is incorrect, please enter again, and % d chances '% (name, 2-j )) j + = 1 # After the password is entered incorrectly, the cycle value is increased by 1 else: lock_file.write (name + '\ n') # After the password is entered incorrectly three times, append the user to the LOCK file sys. exit ('USER % s reaches the maximum number of logins, will be locked and exited' % name) else: pass # When the user does not match, skip and continue loop else: print ('user % s does not exist, please enter again, there are % d opportunities '% (name, 2-I) I + = 1 # When the user input is incorrect, the loop value increases by 1 else: sys. exit ('user % s does not exist, exit '% name) # exit lock_file.close () due to an exception after the user inputs three errors # Close the LOCK file user_file.close () # close the account file

2. users who do not exist in the account file can also be judged and locked. There are three retry attempts with incorrect users and passwords.

The Code is as follows:

# _ * _ Coding: UTF-8 _ * _ import sys, OS, getpassos. system ('clear') retry_limit = 3retry_count = 0account_file = 'account.txt 'lock _ file = 'account_lock.txt' while retry_count <retry_limit: # username = raw_input ('\ 033 [31; 43 mUsername: \ 033 [0m') username = username as long as the retry cannot exceed 3 times. strip () lock_check = open (lock_file) # After the user enters the user name, open the LOCK file to check whether the user has locked the for line in lock_check.readlines (): # loop LOCK file if username = Line. strip ('\ n'): # Remove the linefeed sys. exit (' \ 033 [35 mUser % s is locked !!! \ 033 [0m' % username) # exit password = raw_input ('\ 033 [32; 41 mPassword: \ 033 [0m') if the LOCK is enabled ') # enter the password f = open (account_file, 'R') # open the account file match_flag = False # The default value is Flase. If the user matches, set it to True for line in f. readlines (): user, passwd = line. strip ('\ n '). split () # Remove the redundant \ n of each line and divide this line into two columns by space. assign values to user and passwd variables if username = user and password = passwd: # determine whether the user name and password are equal print ('hello, % s !! '% Username) match_flag = True # if they are equal, the variable match_flag outside the loop is changed to True break # Then the loop does not need to be continued and jumps out directly because f has been matched. close () if match_flag = False: # if the value of match_flag is False, the user name and password are not matched in the above loop. Therefore, print ('Sorry, % s is unmatched '% username) retry_count + = 1 # Add 1 else: print ('Come come login my learning system! ') Break # log on successfully and exit the script else: print ("you account % s is locked !!! "% Username) g = open (lock_file, 'A') g. write (username) # append the locked user to the user lock file g. write ('\ n') g. close ()

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.