Python Practice One

Source: Internet
Author: User

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

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.