Login Interface Exercises
Requirements: Read the account file, login three times the wrong password will lock and write to the lock file
# python 3.6 Login Interface Practice # _*_ coding:utf-8 _*_import sysimport osimport getpassi = 0while i < 3: # continuously loops as long as the user logs in abnormally not more than 3 times name = 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 lock (open with with) with open (' Account_lock.txt ', ' R ') as lock_file : lock_list = lock_file.readlines () for lock_line in lock_list: # Loop Lock File lock_line = lock_line.strip (' \ n ') # Remove line break if name == lock_line: # Exit If lock is the right syS.exit (' User {_name} has been locked, exited '. Format (_name=name)) # user_file = open ( ' Accounts.txt ', ' r ') # open account file (using with open) with open (' Accounts.txt ', ' R ') as user_file : user_list = user_file.readlines () for user_line in user_list: # Traverse the account file (User, password) = user_ Line.strip (' \ n '). Split () # get account and password information separately if name == user: # If the user name matches correctly j = 0 while j < 3: # continuous circulation as long as the user password is not more than 3 times passwd = getpass.getpass (' Please enter password: ') # enter hidden password if passwd == password: # password is correct, prompt Welcome to login print (' Welcome login management Platform, user%s ' % name) sys.exit (0) # Normal Exit else: if j != 2: # j=2 is the last chance to have 0 more chances left in the tip print (' User %s password error, please re-enter, and %d chance ' % (name, 2 - j)) j += 1 # The loop value increases 1 else: after password input error with open (' Account_lock.txt ' A + ') as lock_file : # (opens again using with) lock_file.write (name + ' \ n ') # password entered three times after the error, append the user to the lock file sys.exit (' User %s maximum login count, will be locked and exited ' % name ') else: pass # when the user does not match, Skip and Continue looping else: if i != 2: # i=2 is the last chance to have 0 more chances left in the tip print (' User %s not present, please re-enter, also %d chance ' % (name, 2 - i)) i += 1 # the loop value increases 1else: when the user enters an error Sys.exit (' User %s not present, exit ' % name ') # user entered three errors after the exception exits #lock_file.close () # Close lock file, use with optimization to remove #user_file.close () # Close account file
This article is from the "MG05 Learning Notes ~" blog, please be sure to keep this source http://pimg2005.blog.51cto.com/842469/1980637
Python Practice One