Python: Writing the Login Interface (Day 1)

Source: Internet
Author: User

Job Requirements:

    1. Enter user name, password
    2. Authentication successful Display Welcome message
    3. Lock user after entering error three times

Readme

1.user_id.txt is the file that holds the user ID and password

2.user_lock.txt is the document that holds the locked user ID, which is empty by default.

3. The program will judge the legal user ID in User_id.txt, if the user ID error is entered three times, the program exits directly.

4. The program will enter the correct user name after the password to judge, enter the password has 3 opportunities, three consecutive errors will lock the user ID and exit.

Flow chart:

Code:

#!/user/binenv python3#-*-coding:utf-8-*-"Created on:2015 year January 16 @author: yi Lv Email: [Email protected]version:                                                  1.0Description: Enter the user name password, authentication successful display welcome information, authentication failed, the wrong three times after the lock "' Import sys,os,getpassos.system (' Clear ') #把终端代码页面清空, provide a clean vision i = 0while i < 3: #只要用户登录异常不 OVER 3 cycles username = input ("Please enter user name:") lock_file = open (' User_lock.txt ', ' r+ ') #当用户输入用户名后, open L                                     Ock file to check if this user is already lock lock_list = Lock_file.readlines () for lock_line in Lock_list:                                   #循环LOCK文件 lock_line = Lock_line.strip (' \ n ') #去掉换行符 if username = = Lock_line: #如果LOCK了就直接退出 sys.exit (' =======warning: The account%s has been frozen ======== '% username) u Ser_file = open (' User_id.txt ', ' R ') #打开帐号文件 user_list = User_file.readlines () for User_li            NE in user_list:                         #对帐号文件进行遍历 (User,password) = User_line.strip (' \ n '). Split () #分别获取帐号和密码信息                                            If username = = User: #如用户名正常匹配 m = 0 while M < 3: #只要用户密码异常不超过3次就不断循环 passwd = getpass.getpass (' Please enter password: ') #输入 Password, relative security if passwd = = password: #密码正确, prompt Welcome login print (' hi,%s, welcome                    Login system '% username ' sys.exit (0) #正常退出 else: if M! = 2: #m = 2 o'clock, is the last chance to have 0 more chances left in the prompt print (' User%s secret Code error, please re-enter, there is%d chance '% (username,2-m)) m + = 1 #密码输入错误后, the loop value is increased                1 else:lock_file.write (username + ' \ n ') #密码输入三次错误后, append the user to the lock file Sys.exit (' User%s ' reaches maximumLogin number, please contact admin!! '% username) Else:pass #当用户没匹配时, Skip and continue to follow Ring Else:if I! = 2: #i = 2 o'clock, is the last chance, no more than 0 chances left in the hint print (' User%s does not exist, please re-enter and%d chance '% (username,2-i)) i + = 1 #当用户输入                                                   Error, loop value increases 1else:sys.exit (' user%s does not exist, exits '% username) #用户输入三次错误后, exception exits Lock_file.close () #关闭lock文件user_file. Close () #关闭 user_id file

  

Python: Writing the Login Interface (Day 1)

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.