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!

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft