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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.
