while True means loop forever. The while statement takes an expression and executes the loop body while the expression evaluates to (boolean) "true". True always evaluates to boolean "true" and thus executes the loop body indefinitely. It's an idiom that you'll just get used to eventually! Most languages you're likely to encounter have equivalent idioms.

Note that most languages usually have some mechanism for breaking out of the loop early. In the case of Python it's the break statement in the cmd == 'e' case of the sample in your question.

Answer from Richard Cook on Stack Overflow
Discussions

What actually "while true" statement mean can someone pull me out this?
What actually "while true" statement mean can someone pull me out this · In practical terms, it will loop a code block, while a particular condition is True. Said condition may never be True, in which case you get an infinite loop, or you can set and test a variable so that you have a fixed ... More on discuss.python.org
🌐 discuss.python.org
0
0
April 30, 2022
Can someone explain what this `while True` function is actually checking?
A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ") More on reddit.com
🌐 r/learnpython
14
17
December 13, 2023
Repeat while loop?
Hello! So, Im trying to figure out how to repeat a while loop. I want my loop to restart completely, like if I’m making a game and I want a round two. I’ve tried looking online but it always confused me what is going on… anyone mind helping me out? More on discuss.python.org
🌐 discuss.python.org
0
0
October 28, 2021
python - How to emulate a do-while loop? - Stack Overflow
I need to emulate a do-while loop in a Python program. Unfortunately, the following straightforward code does not work: list_of_ints = [ 1, 2, 3 ] iterator = list_of_ints.__iter__() element = None while True: if element: print element try: element = iterator.next() except StopIteration: break ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
freecodecamp.org › news › python-do-while-loop-example
Python Do While – Loop Example
August 31, 2021 - The while loop, on the other hand, doesn't run at least once and may in fact never run. It runs when and only when the condition is met. So, let's say we have an example where we want a line of code to run at least once. secret_word = "python" counter = 0 while True: word = input("Enter the secret word: ").lower() counter = counter + 1 if word == secret_word: break if word != secret_word and counter > 7: break
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-while-true-in-python
How to use while True in Python - GeeksforGeeks
July 23, 2025 - A while loop in Python repeatedly executes a block of code as long as the specified condition evaluates to True. ... You use a break statement to exit the loop. An exception or program exit occurs inside the loop.
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - If you come from languages like C, C++, Java, or JavaScript, then you may be wondering where Python’s do-while loop is. The bad news is that Python doesn’t have one. The good news is that you can emulate it using a while loop with a break statement. Consider the following example, which takes user input in a loop: ... >>> while True: ...
🌐
Board Infinity
boardinfinity.com › blog › use-while-true-in-python
Use While True in Python | Board Infinity
August 13, 2025 - To use a "while True" loop in Python, you must first define a condition that will eventually evaluate to "False" for the loop to terminate. This is typically done using a Boolean variable initially set to "True," then modified within the loop ...
Find elsewhere
🌐
Python.org
discuss.python.org › python help
What actually "while true" statement mean can someone pull me out this? - Python Help - Discussions on Python.org
April 30, 2022 - What actually "while true" statement mean can someone pull me out this · In practical terms, it will loop a code block, while a particular condition is True. Said condition may never be True, in which case you get an infinite loop, or you can ...
🌐
Reddit
reddit.com › r/learnpython › can someone explain what this `while true` function is actually checking?
r/learnpython on Reddit: Can someone explain what this `while True` function is actually checking?
December 13, 2023 -

https://pastebin.com/NWkKQc1P

It's from Automate the Boring Stuff. I tried running it through the python tutor and it didn't clarify. Is the basic point that there actually isn't anything it's checking to be True, so it's an infinite loop until you trigger the `break`?

PS I figure this is something simple from much earlier that I didn't internalize. I plan to go through all the basic curriculum again to make sure there aren't any gaps in super basic knowledge.

Top answer
1 of 8
23
A while loop runs while a condition is true. True is always true, so it will run until something causes it to break (e.g. a break statement, program crash, etc.) For comparison, the first loop in the code you posted could be rewritten without an infinite loop as: age = input("Enter your age: ") while not age.isdecimal(): age = input("Please enter a number for your age: ")
2 of 8
9
Correct; this is just a way to start an infinite loop that you intend to break with some condition/break statement later on. Mostly used for starting/running an application that you don't want to close down without user intent (clicking an exit/quit button in a UI or entering "quit", "exit", etc from the command line. It *isn't* a great construct to use when you want something that has solidly defined criteria to repeat until those criteria are met. As mopslik pointed out, you would be better served in those examples to actually use the defined criteria in those as your loop conditions since you're not at risk of just sticking the user in an infinite loop if you forget to write your break statement somewhere. You, also, don't have to write so many lines to accomplish the same goal. ``` password = "" while not password.isalnum(): password = input('Select a new password (alphanumeric only') print("Password is good") ``` You do not have to print a line and then call input(), either. Just put the message you want displayed into the parens for the function call: input("Enter your age: ")
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Bootcamp Python Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true....
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example: >>> while True: ...
🌐
Python.org
discuss.python.org › python help
Repeat while loop? - Python Help - Discussions on Python.org
October 28, 2021 - Hello! So, Im trying to figure out how to repeat a while loop. I want my loop to restart completely, like if I’m making a game and I want a round two. I’ve tried looking online but it always confused me what is going on……
🌐
freeCodeCamp
freecodecamp.org › news › while-loops-in-python-while-true-loop-statement-example
While Loops in Python – While True Loop Statement Example
July 19, 2022 - If the condition evaluates to True, ... the code while the condition remains True. It will keep executing the desired set of code statements until that condition is no longer True. Let’s take a hypothetical example. You may ask a user to submit a secret keyword so they can access a specific part of your site. Say that for them to be able to view some content, they first have to enter the keyword ‘Python’. To do this, you ...
🌐
TOOLSQA
toolsqa.com › python › python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - Python "Do While" loops. The while loop in python is a way to run a code block until the condition returns true repeatedly. Unlike the "for" loop in python, the while loop does not initialize or increment the variable value automatically.
🌐
GeeksforGeeks
geeksforgeeks.org › python-do-while
Python Do While Loops - GeeksforGeeks
August 12, 2024 - Printing list items using while loop geeksforgeeks C++ Java Python C MachineLearning Printing list items using do while loop geeksforgeeks C++ Java Python C ... total = 0 # loop will run at least once while True: # ask the user to enter a number num = int(input("Enter a number (or 0 to exit): ")) # exit the loop if the user enters 0 if num == 0: break total += num # print the total print("Total:", total)
🌐
Texas Instruments
education.ti.com › en › resources › computer-science-foundations › do-while-loops
Python coding: Do While Loops | Texas Instruments
Next, a Do While loop (modeled using a While loop with a condition of True) kicks in. Within the loop, a new variable n is created and it’s assigned a random number from 1–6. Also, the value of c is increased by one to show that a new number has been randomly determined.
🌐
YoungWonks
youngwonks.com › blog › do-while-loop-python
What is a do while loop in Python? How do I create a while loop in Python? What is the difference between a for loop and a while loop in Python?
February 18, 2024 - Here's how you can write a do-while loop in Python: The while True statement creates an infinite loop, which will run indefinitely unless we stop it. The if not conditional statements checks the opposite of the loop condition we want to use ...
🌐
DataCamp
datacamp.com › tutorial › do-while-loop-python
Emulating a Do-While Loop in Python: A Comprehensive Guide for Beginners | DataCamp
January 31, 2024 - To emulate a "do-while" loop in Python, you can use a while loop with a True condition and strategically place a break statement.
🌐
Python Forum
python-forum.io › thread-28042.html
Is using while True loop good?
Hi, I've created a process in which I've used one while True loop for watch on a changeable variable. If the variable change something happens, otherwise the while loop rotates blank. This process is intended to be used in comparatively low resou...
🌐
Python.org
discuss.python.org › python help
Infinte while loop using try/except - Python Help - Discussions on Python.org
June 15, 2024 - hey y’all! I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure. I’m running into an infinite loop when the exceptions a…