Backend Development
Python Tutorial
Teach you step by step how to use the while True statement in python
In the learning process, we often encounter the usage of while True. The following is an example to illustrate:
Establish a user login system. The user enters the user name and password. If correct, they can enter the system.
1. My initial writing method:
d = {} #数据库字典,所有用户的用户名密码存储在此
name = input("请输入您的用户名:")
if name in d:
password = input("请输入您的密码")
if d[name] == password:
print('进入系统')
else:
print('您输入的密码错误,请重新输入')
else:
print('您输入的用户名不正确,请重新输入')This program I wrote seems to be logically correct:
——Let first The user enters a username. If the username exists, the user continues to enter a password. If the password is correct, the system is entered. If the password is incorrect, re-enter it.
——If the user name does not exist, it will prompt "The user name you entered is incorrect, please re-enter"
However, during the actual operation, if the user name and password entered by the user are both Correct, of course there is no problem. But if any input is incorrect, the system will only display "The user name you entered is incorrect, please re-enter" or "The password you entered is incorrect, please re-enter".
In other words, if an error occurs and there is no return to the original place, the request is made to continue input.
2. Use the while True loop statement:
The core idea of using this statement is that if an error occurs, the loop can continue.
d = {}
while True:
name = input('请输入您的用户名:')
if name in d:
break
else:
print('您输入的用户名不存在,请重新输入')
continue
while True:
password = input('请输入您的密码:')
if d[name] == password:
print('进入系统')
break
else:
print('您输入的密码不正确,请重新输入')
continueThe above program can achieve the desired purpose: when the user name or password is entered incorrectly, return to the beginning and request continued input.
The while True statement must have a break statement to end the loop, otherwise the loop will continue.
Of course, we have encountered that if the password is wrong, you will be prompted to have several chances to enter the password. It can be achieved like this:
count = 5
while count:
password = input('请输入您的密码:')
if d[name] == password:
print('进入系统')
break
else:
count -= 1
print('您输入的密码不正确,还有{}次输入机会'.format(count))
continueThank you everyone for reading, I hope you will benefit a lot.
This article is reproduced from: https://blog.csdn.net/geerniya/article/details/77524173
Recommended tutorial: "python tutorial"
The above is the detailed content of Teach you step by step how to use the while True statement in python. For more information, please follow other related articles on the PHP Chinese website!
Python and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AMTo maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.
Python: Games, GUIs, and MoreApr 13, 2025 am 12:14 AMPython excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.
Python vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AMPython is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.
The 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AMYou can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.
Python: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AMPython is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.
How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PMYou can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.
How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AMHow to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...
How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AMHow to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software





