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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-use-while-true-in-python
How to use while True in Python - GeeksforGeeks
July 23, 2025 - In Python, loops allow you to repeat code blocks. The while loop runs as long as a given condition is true. Using while True creates an infinite loop that runs endlessly until stopped by a break statement or an external interruption.
Discussions

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
sorry to ask such a silly question but what does 'while True:' actually mean?
while will always evaluate whatever comes behind it. For instance i =0 while i < 10: print(i) i = i+ 1 Here i <10 is evaluated to either be true or false. This will happen each iteration. i = 0 , True i= 1, True i= 2, True Etc, untill: i=10, False => break loop The code will continue untill it evaluates to false. while True kind of hacks it: True will always evaluate to True. While looks at it and thinks: hey this is true! So it always continues. While true isn't commonly used, but when it is it's commonly used with a break statement. (Or interrupted by ctrl + c). While False will never do anything in the loop as you stated. It checks to see if False is True. But it's never true, so it doesnt execute the code in the while loop. (Sorry fod bas formatting, mobile) More on reddit.com
๐ŸŒ r/learnpython
71
76
November 14, 2023
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
python - Using While True loops - Stack Overflow
I am supposed to write a code that checks a word for certain syllables (examples: in, ex, are). If the word is made up of only those syllables, the code returns "Yes" and "No" otherwise (if the wor... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
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 loop's body and continue to run the code while the condition remains True. It will keep executing the desired set of code statements until that condition is no longer True.
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The basic syntax of a while loop is shown below: ... In this syntax, condition is an expression that the loop evaluates for its truth value. If the condition is true, then the loop body runs.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
Python Examples Python Compiler ... Certificate Python Training ... With the while loop we can execute a set of statements as long as a condition is true. ... Note: remember to increment i, or else the loop will continue ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ while-loop
Python while Loop (With Examples)
# Print numbers until the user ... it is printed. If the user enters 0, the loop terminates. If the condition of a while loop always evaluates to True, the loop runs continuously, forming an infinite while loop....
Find elsewhere
๐ŸŒ
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: ")
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ sorry to ask such a silly question but what does 'while true:' actually mean?
r/learnpython on Reddit: sorry to ask such a silly question but what does 'while True:' actually mean?
November 14, 2023 -

what does the True refer to?

eg if i write:

while True: 
    print("hi")

i know that "hi" will be repeated infinitely, but what is it thats True thats making "hi" get printed?

also, whatever it is thats True, im assuming its always true, so is that why if i typed 'while False', none of the code below would ever run?

sorry if this is a stupid question

edit: also, besides adding an if statement, is there anything else that would break this infinite while loop?

and how would i break the loop, lets say after "hi" has been printed 10 times, using an if statement or whatever else there is that can break it. thanks!

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-while-loop-tutorial
Python While Loop Tutorial โ€“ While True Syntax Examples and Infinite Loops
November 13, 2020 - Instead of writing a condition after the while keyword, we just write the truth value directly to indicate that the condition will always be True. ... >>> while True: print(0) 0 0 0 0 0 0 0 0 0 0 0 0 0 Traceback (most recent call last): File ...
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ python โ€บ python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - We then write the break statements inside the code block. The while true in python is simple to implement. Instead of declaring any variable, applying conditions, and then incrementing them, write true inside the conditional brackets.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
The while-loop syntax has 4 parts: while, boolean test expression, colon, indented body lines: ... While Operation: Check the boolean test expression, if it is True, run all the "body" lines inside the loop from top to bottom. Then loop back to the top, check the test again, and so on.
๐ŸŒ
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 ...
๐ŸŒ
Readthedocs
pc-microbit-micropython.readthedocs.io โ€บ en โ€บ latest โ€บ lessons โ€บ while_True_loops.html
2. while True loops โ€” PC-Microbit-Micropython - Read the Docs
The statements within the loop are indented with the tab key (which is equivalent to 4 spaces) to make those lines part of the while-loop. The while True loop below scrolls the text I never stop across the LED display, over and over again. from microbit import * while True: display.scroll('I ...
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python While True Loop Explained with 3 Easy Code Examples 2025 - YouTube
Learn how to use the Python while True loop in this beginner-friendly tutorial! ๐Ÿš€ We'll break down what a while True loop is, why it's useful, and show you ...
Published ย  January 21, 2025
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-while-loop
How to Write and Use Python While Loops | Coursera
If the test expression evaluates to true, the body of the while loop will be entered. This process repeats until the test expression evaluates to false. ... 1 2 3 4 5 numbers = [2, 4, 6, 8, 10] total = 0 for num in numbers: total += num print("The ...
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to use 'while true' in python
How to use 'while True' in Python | Replit
1 month ago - When the input matches 'quit', the break statement is executed, providing a clean and predictable way to terminate the otherwise infinite loop. While break provides a simple exit, you can achieve finer control by combining it with continue to validate user input or build complex interactive menus. counter = 0 while True: counter += 1 if counter % 2 == 0: # Skip even numbers continue print(counter) if counter >= 5: break--OUTPUT--1 3 5
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
As the for loop in Python is so powerful, while is rarely used, except in cases where a user's input is required*, for example: n = raw_input("Please enter 'hello':") while n.strip() != 'hello': n = raw_input("Please enter 'hello':") However, the problem with the above code is that it's wasteful. In fact, what you will see a lot of in Python is the following: while True: n = raw_input("Please enter 'hello':") if n.strip() == 'hello': break