This article mainly introduces the Python implementation of the Login interface sample code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
Before writing the Python implementation of the Login Interface sample code, recently need to review, and then sent to the essay
Requirements:
1. Enter your user name and password
2. Successful authentication, display welcome information
3. After the user name 3 times input error, exit the program
4. Password 3 times 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 inquires 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 the 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:
# Joe Youngimport os, sys, Getpassos.system (' cls ') #调用os模块的system方法传入 ' cls ' parameter, clear screen count = 0 #用户名登录次数计数while cou NT < 3:username = input (' username: ') lock_file = open (' LockList.txt ', ' r+ ') #打开LockList. txt file, permissions r+ (open for Read and write files.) The file pointer is placed at the beginning of the file) Lock_list = Lock_file.readlines () #使用readlines () method reads the LockList.txt, generates the list, and assigns a value to lock_list for Lock_line I N lock_list:if Username = = Lock_line.strip (' \ n '): #使用strip () method removes the newline character and determines whether username is in LockList.txt print (' User name%s ' is locked , please contact administrator ... '% (username)) Sys.exit (1) The #sys模块的exit () method means to exit with open (' UserList.txt ', ' R ') as User_file: # Open UserList.txt, permissions read-only user_list = User_file.readlines () #逐行读取UserList. txt file, assign to user_list variable for user_line in User_li ST: (user, passwd) = User_line.strip (' \ n '). Split (': ') #获取user, passwd value, using Split (': ') to implement the split string if user = = Username: #判断用户名是否在UserList. txt file in n = 0 #密码输入次数计数 while n < 3: #3次输入 Opportunity Password = Getpass.Getpass (' Password: ') #使用getpass模块的getpass () method gets the user-entered password if password = = passwd: #判断密码是否匹配 print ( ' Welcome to%s login System! '% (username)) sys.exit (0) else:if n! = 2: #n = 2 o'clock, is the last chance, there are 0 chances left without prompting P Rint (' Password wrong, please re-enter, you have%d chance \ n '% (2-n)) n + = 1 #密码输入错误, number of times +1 else:lock_file.write (user name + ' \ n ') #密码输入错误次数达到3次, write the user name to the LockList.txt file, lock the username sys.exit (' Too many errors, the user name is locked ... ') #程序退出, and output the prompt else: #用户名不存在, execute the Else statement if count! = 2: #count = 2 o'clock, is the last chance to enter a user name without prompting for 0 chances left print (' User name does not exist, please try again, you have%d chances \ n '% (2-count)) Count + = 1 #用户名输入错误, Count+1else: #用户名输入错误 The number of times reached 3 sys.exit (' Too many entries, program has exited ... ') #退出程序 and output prompt lock_file.close () #关闭LockList. txt file