Job: Writing the Login interface
- Enter User name password
- Show welcome message after successful authentication
- Three-time error after locking
There are two types of scripts written for this instance, slightly different, as follows:
Account file Account.txt content as follows:
Sam 123
David 12
Kevin 123
Lin 12
Tailen 123
Jack 12
Lock file account_lock.txt default is empty
One, only for the user in the account file to determine and lock, for the user and password each have three error retry opportunities.
1, the flowchart is as follows:
The code is as follows:
#!/usr/bin/python27#_*_ coding:utf-8 _*_ImportSys,os,getpassos.system ('Clear') I=0 whileI < 3:#keep looping as long as the user login exception is no more than 3 timesName = Raw_input ("Please enter user name:") Lock_file= Open ('Account_lock.txt','r+')#when the user enters the user name, open the lock file to check if the user is already lockLock_list =Lock_file.readlines () forLock_lineinchLock_list:#Loop Lock FileLock_line = Lock_line.strip ('\ n')#Remove line break ifName = = Lock_line:#if Lock's on, exit directly.Sys.exit ('The user%s has been locked out'%name) User_file= Open ('Account.txt','R')#Open Account FileUser_list =User_file.readlines () forUser_lineinchUser_list:#traverse the account file(User,password) = User_line.strip ('\ n'). Split ()#Get account and password information separately ifName = = User:#If the user name matches correctlyj =0 whileJ < 3:#keep looping as long as the user's password is not more than 3 times abnormalpasswd = Getpass.getpass ('Please enter your password:')#Enter hidden password ifpasswd = = Password:#password is correct, prompt welcome login Print('Welcome to login management Platform, user%s'%name) sys.exit (0)#Normal Exit Else: Print('User%s password is wrong, please re-enter, there are%d chances'% (name,2-j)) J+ = 1#1 increase in loop value after password input error Else: Lock_file.write (name+'\ n')#After you enter the password three times, append the user to the lock fileSys.exit ('The user%s has reached the maximum number of logins and will be locked out'%name)Else: Pass #Skip and Continue looping when the user does not match Else: Print('user%s does not exist, please re-enter and%d chance'% (name,2-i)) I+ = 1#the loop value increases by 1 when the user enters an errorElse: Sys.exit ('user%s does not exist, exit'% name)#the user enters the error three times and exits unexpectedlylock_file.close ()#Close the lock fileUser_file.close ()#Close Account File
Second, for the account file does not exist in the user can also be judged and locked, for the user and password a total of three error retry opportunities
The code is as follows:
#_*_ coding:utf-8 _*_ImportSys,os,getpassos.system ('Clear') Retry_limit= 3Retry_count=0account_file='Account.txt'Lock_file='Account_lock.txt' whileRetry_count < retry_limit:#keep looping as long as you retry no more than 3 timesUsername = Raw_input ('\033[31;43musername:\033[0m') Username=Username.strip () Lock_check= Open (Lock_file)#when the user enters the user name, open the lock file to check if the user is already lock forLineinchLock_check.readlines ():#Loop Lock File ifUsername = = Line.strip ('\ n'):#Remove line breakSys.exit ('\033[35muser%s is locked!!! \033[0m'% username)#if Lock's on, exit directly.Password = raw_input ('\033[32;41mpassword:\033[0m')#Enter PasswordF= Open (Account_file,'R')#Open Account FileMatch_flag = False#The default is Flase, which is set to True if the user match is on forLineinchf.readlines (): user,passwd= Line.strip ('\ n'). Split ()#remove each line of extra \ n and divide the line into two columns, each assigned a value of USER,PASSWD two variables ifUsername = = User andPassword = = passwd:#determine if the user name and password are equal Print('Hello,%s !!!'%username) Match_flag= True#equality changes the Match_flag variable outside the loop to true Break #and then you don't have to keep looping, just jump out, because it's already on match.f.close ()ifMatch_flag = = False:#if the Match_flag is false, it will not match the username and password on the previous loop, so you need to continue looping Print('sorry,%s is unmatched'%username) Retry_count+ = 1#counter plus 1 Else: Print('wlecome Login My Learning system!') Break #user successfully logged in, Exit scriptElse: Print("Your account, %s is locked!!!"%username) G= Open (Lock_file,'a') G.write (username)#the lock user is appended to the user lock fileG.write ('\ n') G.close ()
Python's Job Writing login interface (first day)