Python Implements the Login interface

Source: Internet
Author: User

Requirements:

    1. Enter user name and password
    2. Successful authentication, display welcome information
    3. User Name 3 input error, exit the program
    4. Password 3 input error, lock user name

Readme:

    1. UserList.txt is the file that holds the user name and password, in the format: Username:password, each line holds a user information
    2. LockList.txt is a file that holds a locked user name and is empty by default
    3. The user enters the user name, the program first queries the lock list LockList.txt, if the user name is inside, prompts the user to be locked, and exits the program
    4. If the user name is not in the lock list, the program will query the user list UserList.txt, if the user name is not inside, will prompt the user does not exist, please re-enter, three times input error, will exit the program
    5. If the user name is in the user list, will prompt the user to enter the password, the password is correct, displays the welcome message, 3 times the input error, will lock this user name (writes the lock list)

Flow chart:

         

Code:

1 #Joe Young2 3 4 ImportOS, sys, Getpass5 6Os.system ('CLS')#call the OS module's system method to pass in the ' cls ' parameter, clear the screen7 8Count = 0#User name logon Count9 Ten  whileCount < 3: One  AUsername = input ('Username:') -  -Lock_file = open ('LockList.txt','r+')#Open the LockList.txt file, permissions r+ (open for Read and write files. The file pointer is placed at the beginning of the file) theLock_list = Lock_file.readlines ()#Use the ReadLines () method to read the LockList.txt line by row, generate a list, and assign a value to Lock_list -  -      forLock_lineinchlock_list: -         ifUsername = = Lock_line.strip ('\ n'):#use the Strip () method to remove line breaks to determine if username is LockList.txt +             Print('User name%s is locked, please contact Administrator ...'%(username)) -Sys.exit (1)#the exit () method of the Sys module indicates an exit +  AWith open ('UserList.txt','R') as User_file:#Open UserList.txt, permission Read Only atUser_list = User_file.readlines ()#read UserList.txt file line by row, assign to user_list variable -  -      forUser_lineinchuser_list: -(user, passwd) = User_line.strip ('\ n'). Split (': ')#gets the value of the USER,PASSWD, using split (': ') to implement the split string -         ifuser = = Username:#determine if the user name is within the UserList.txt file -n = 0#count of password input times in              whileN < 3:#3-Time input opportunities -Password = Getpass.getpass ('Password:')#Use the Getpass () method of the Getpass module to get the password entered by the user to                 ifPassword = = passwd:#determine if the password matches +                     Print('Welcome to%s login System! '%(username)) - sys.exit (0) the                 Else: *                     ifn! = 2:#n=2 is the last chance, no hint, 0 chances left. $                         Print('password error, please re-enter, you have%d chance \ n'% (2-N))Panax Notoginsengn + = 1#Password input error, number of times +1 -             Else: theLock_file.write (username +'\ n')#Password input error number reached 3 times, the user name is written to the LockList.txt file, lock the user name +Sys.exit ('excessive number of errors, user name is locked ...')#program exits, and outputs hints A     Else:#user name does not exist, execute else statement the         ifCount! = 2:#count=2, is the last chance to enter a username, no hint, there are 0 chances left. +             Print('user name does not exist, please try again, you have%d chances \ n'% (2-count)) -Count + = 1#User Name input error, count+1 $  $ Else:#User Name input error number reached 3 times -Sys.exit ('too many entries, the program has exited ...')#exit the program and output hints -  theLock_file.close ()#Close LockList.txt file

Python Implements 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.