Home > Article > Backend Development > Sharing on how to implement the login interface in Python
This article mainly introduces the sample code for implementing the login interface in Python. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
I wrote a sample code for implementing the login interface in Python before. I need to review it recently, so I posted it to the essay by the way.
Requirements:
1. Enter username and password
2. Authentication successful, welcome message is displayed
3. User name 3 times After entering the wrong password, exit the program
4. After entering the wrong password three times, the user name will be locked
Readme:
1.UserList.txt is a file that stores usernames and passwords. The format is: username: password. Each line stores one piece of user information
2.LockList.txt is a file that stores locked usernames. The default is empty
3. The user enters the user name, and the program first queries the lock list LockList.txt. If the user name is in it, it 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 in the lock list, it will prompt that the user does not exist. Please re-enter. If you enter incorrectly three times, the program will exit
5. If the username is in the user list, the user will be prompted to enter the password. If the password is correct, a welcome message will be displayed; if the user name is entered incorrectly three times, the username will be locked (written into the lock list)
Flowchart:
Code:
# Joe Young import os, sys, getpass os.system('cls') #调用os模块的system方法传入'cls'参数,清屏 count = 0 #用户名登录次数计数 while count < 3: username = input('username:') lock_file = open('LockList.txt', 'r+') #打开LockList.txt文件,权限r+(打开用于读和写文件。文件指针置于该文件的开头) lock_list = lock_file.readlines() #使用readlines()方法逐行读取LockList.txt,生成列表,并赋值给lock_list for lock_line in lock_list: if username == lock_line.strip('\n'): #使用strip()方法去掉换行符,判断username是否在LockList.txt print('用户名 %s 已被锁定,请联系管理员...' %(username)) sys.exit(1) #sys模块的exit()方法表示退出 with open('UserList.txt', 'r') as user_file: #打开UserList.txt,权限只读 user_list = user_file.readlines() #逐行读取UserList.txt文件,赋值给user_list变量 for user_line in user_list: (user, passwd) = user_line.strip('\n').split(': ') #获取user,passwd的值,用split(': ')实现分割字符串 if user == username: #判断用户名是否在UserList.txt文件内 n = 0 #密码输入次数计数 while n < 3: #3次输入机会 password = getpass.getpass('password:') #使用getpass模块的getpass()方法获取用户输入的密码 if password == passwd: #判断密码是否匹配 print('欢迎 %s 登陆系统!' %(username)) sys.exit(0) else: if n != 2: #n=2时,是最后一次机会,不需要提示还剩下0次机会 print('密码错误,请重新输入,您还有 %d 次机会\n' %(2-n)) n += 1 #密码输入错误,次数+1 else: lock_file.write(username + '\n') #密码输入错误次数达到3次,把用户名写入LockList.txt文件,锁定用户名 sys.exit('错误次数过多,用户名已被锁定...') #程序退出,并输出提示 else: #用户名不存在,执行else语句 if count != 2: #count=2时,是最后一次输入用户名的机会,不用提示还剩下0次机会了 print('用户名不存在,请重试,您还有 %d 次机会\n' %(2-count)) count += 1 #用户名输入错误,count+1 else: #用户名输入错误次数达到3次 sys.exit('输入次数过多,程序已退出...') #退出程序,并输出提示 lock_file.close() #关闭LockList.txt文件
The above is the detailed content of Sharing on how to implement the login interface in Python. For more information, please follow other related articles on the PHP Chinese website!