"Follow the Golden Horn king to learn Python"-after-school assignment: writing the Login interface

Source: Internet
Author: User

Requirements:

    • Enter User name password

    • Show welcome message after successful authentication

    • Three-time error after locking

This process and job requirements are slightly different when the user enters the same user name three times and cannot log on when the user name is locked.

The following will also be attached to the code to meet the requirements, but there is a bug, that is, enter three times can not log in correctly only the last time the user name is locked.


Program One:

#! usr/bin/evn python#-*-coding utf-8  -*-__author__ =  ' Galen ' Import getpass  as gp# introduced the Getpass module import pickle# introduced the Pickle module, Pickle provides a simple persistence function. You can store the object as a file on disk. import os# verifies that there is a file that holds the locked user, if not, creates an empty file and writes an empty list if not os.path.exists ("F:/AAAA Data/python Program/ python3.5-0 Basics-Advanced-complete Project anatomy/1th Week/lock_user.txt "):     lock_user_file = open (" Lock_ User.txt "," WB ")     lock_user = []    pickle.dump (Lock_user, Lock_user_file)     lock_user_file.close () #定义事先设定的username和password常亮_username  = "Galen "_password = " Galen "inpu_user = {} #创建一个空的字典, used to determine a user name entered several times count = 0# The number of times to record input While count<3:    print ("{} times Input". Format (count+1))      Username = input ("username:")     if username not in inpu_ user:        inpu_user[username]=0     #读取文件     lock_user_file =  Open ("Lock_user.txt", "RB")     lock_user = pickle.load (lock_user_file)      lock_user_file.close ()      #验证输入的用户是否已经被锁定     if  username not in lock_user:        password =  gp.getpass ("Password:")         if username == _ username and password == _password:             print ("Welocme {} come back". Format (username))              count +=1             inpu_user[username] +=1             break        else:             print ("You are illegal user")              count +=1            inpu_user[ username] +=1    else:         #如果输入的用户被锁定, You can verify the unlock         print ("You have been locked,please  contact with administrators ")         #         if input ("are you want to contact  administrators : ")  ==" y ":             if  input ("please input your ide_code : ")  == "Galen":                  #输入输入验证码正确则, delete locked username                  lock_user_file = open ("Lock_user.txt", "RB")                  lock_user =  Pickle.load (Lock_user_file)                  lock_user.remove (username)                  lock_user_file = open ("Lock_user.txt", "WB")                  pickle.dump (Lock_user,lock_user_file)                 lock_user_ File.close ()             else:          &Nbsp;      print ("You have lost the last chance")          break# Lock the user if you have entered three consecutive times without logging in if inpu_user[username]==3:     lock_user_file = open ("Lock_user.txt", "RB")     lock_user =  pickle.load (lock_user_file)     lock_user.append (username)      Lock_user_file = open ("Lock_user.txt", "WB")     pickle.dump (Lock_user,lock_user_ File)     lock_user_file.close ()

Program two:

#! usr/bin/evn python#-*-coding utf-8  -*-__author__ =  ' Galen ' Import getpass  as gp# introduces the Getpass module import pickleimport os# verifies that there is a file that holds the locked user, if not, creates an empty file and writes an empty list if  Not os.path.exists ("F:/AAAA data/python program/python3.5-0 Basics-Advanced-complete Project anatomy/1th Week/lock_user.txt"):     lock_user_file = open ("Lock_user.txt", "WB")     lock_user = []     pickle.dump (Lock_user,lock_user_file)     lock_user_file.close () # Define pre-set username and password solid _username = "Galen" _password =  "Galen" #用于记录输入的次数count  =  0while count<3:    print ("{} times Input". Format (count+1))     username  = input ("username:")      #读取文件     lock_user_file =  open ("Lock_user.txt", "RB")     lock_user = pickle.load (Lock_user_file)     lock_user_File.close ()      #验证输入的用户是否已经被锁定     if username not in  lock_user:        password = gp.getpass ("Password:")         if username == _username and password  == _password:            print ("Welocme  {} come back ". Format (username))              count +=1            break         else:             print ("You are illegal user")              count +=1    else:        # If the user entered is locked, it can be unlocked by verifying        print ("You have been locked,please contact  with administrators ")         #         if input ("ARE&NBSP;YOU&NBSP;WANT&NBSP;TO&NBSP;CONTACT&NBSP;ADMINISTRATORS&NBSP;:" )  == "Y":             if input ("please  input your ide_code :  ")  ==" Galen ":                  #输入输入验证码正确则, delete the locked user name                  lock_user_file = open ("Lock_user.txt", " RB ")                 lock_user  = pickle.load (Lock_user_file)                 &nbsP;lock_user.remove (username)                  lock_user_file = open ("Lock_user.txt", "WB")                  pickle.dump (Lock_user,lock_user_file)                  lock_user_file.close ()             else:                 print ("You have lost the  last chance ")         break# lock the last user you entered if you entered three times without logging in if  Count == 3 :    lock_user_file = open ("Lock_user.txt", "RB")      lock_user = pickle.load (lock_user_file)     lock_user.append ( username)     loCk_user_file = open ("Lock_user.txt", "WB")     pickle.dump (Lock_user,lock_user_ File)     lock_user_file.close ()


This article is from the "Pure mash Eight cup vsit" blog, please be sure to keep this source http://chunlaobazhong.blog.51cto.com/11499723/1956085

"Follow the Golden Horn king to learn Python"-after-school assignment: writing the Login interface

Related Article

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.