Jobs: Writing Login Interfaces
- Enter User name password
- Show welcome message after successful authentication
- Three-time error after locking
For this instance, write the following details:
Account file Account.txt content as follows:
Tamir 123
Test1 123
Test2 123
TEST3 123
Account Lock file account_lock.txt default is empty
The user in the account file (Account.txt) is judged and locked, and there are three error retry opportunities for the user and password.
1. The flowchart is as follows:
The code is as follows:
1 #-*-coding:utf-8-*-2 #Author:steven Kang3 4 ImportOs,sys,getpass#Import Os,sys,getpass Module5U = 0#number of cycles for users6 whileU < 3:7Username = input ("Please enter your user name:")#use input to let the user enter and assign a value to the username variable8Lock_file = open ('Account_lock.txt','r+')#Open the Account_lock file with permission to read the update and assign a value to the Lock_f variable9Lock_list = Lock_file.readlines ()#use the. ReadLines method to read the Account_lock file row by line and assign a value to the lock_list variableTen One forLock_lineinchLock_list:#to read the contents of a account_lock using a for loop ALock_line = Lock_line.strip ('\ n')#use the. Strip () method to go to point line breaks - ifUsername = =Lock_line: - Print('user%s is locked, please contact your system administrator'% username)#If there is print output theSys.exit (1)#Jump out of the loop -User_file = open ('Account.txt','R')#Open account file, permissions are read update, and assigned to user_f variable -User_list = User_file.readlines ()#use the. ReadLines method to read the account file row by line and assign a value to the user_list variable - forUser_lineinchUser_list:#to read the contents of an account using a For loop +(user,passwd) = User_line.strip ('\ n'). Split ()#Get account and password information separately - ifUsername = = User:#use if to determine if user input user is present in Normal_user +p = 0#number of cycles to enter a password A whileP < 3:#keep looping as long as the user login exception is no more than 3 times atPassword = Getpass.getpass ('Please enter your password:')#use the. Getpass method of the Getpass module to allow the user to enter a password (not shown when entered) - ifPassword = = passwd:#use if to determine if the password entered by the user is present in the Normal_user (equal) - Print('Welcome to%s login System'%username)#User name password all equal (presence) Print Welcome login Information -Sys.exit (0)#Jump out of the loop - Else: - ifP! = 2: in Print('Sorry, the password for%s is wrong, please re-enter, you have%d chances'% (username,2-p)) -p + = 1#1 increase in loop value after password input error to Else: +Lock_file.write (username +'\ n')#User name password entered more than 3 times the user added to the Account_lock file - #lock_f.write ('%s \ n '%username) theSys.exit ('Sorry the%s user is locked out, please contact the Administrator'%username) * Else: $ Pass #Skip and Continue looping when the user does not matchPanax Notoginseng Else: - ifU! = 2:#i=2 is the last chance to have 0 more chances left in the tip. the Print('Sorry,%s input error, please re-enter, you have%d chance'% (username, 2-u)) +U + = 1#the loop value increases by 1 when the user enters an error A Else: theSys.exit ('The program has exited because of your error input, please rerun')#the user enters the error three times and exits unexpectedly +Lock_f.close ()#Close Lock_f file
Python Writing Login interface (first day)