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
๐ŸŒ
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 9
24
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 9
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
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1. With the break statement we can stop the loop even if the while condition is true:
Discussions

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
python - Close a While True Loop from another While True - Stack Overflow
The issue is that the loop (While True) I made to keep the app running after an exception, is what now denies closing the app. So if you type "exit", the app restarts and go back to start the loop. I've tried many things, most of them actually stupid, with dead-ends... So not worth describing. I am really green not only at Python... More on stackoverflow.com
๐ŸŒ stackoverflow.com
while True ?
Kostas Koutoupis is having issues with: What does while True do in this function? What value does it check for/checks against? I'm referring to the coding solution found ... More on teamtreehouse.com
๐ŸŒ teamtreehouse.com
1
September 21, 2019
What does "while True" mean?
Subreddit for posting questions and asking for general advice about all topics related to learning python. More on reddit.com
๐ŸŒ r/learnpython
7
3
January 10, 2021
๐ŸŒ
Board Infinity
boardinfinity.com โ€บ blog โ€บ use-while-true-in-python
while True in Python - How It Works & Best Practices
June 16, 2026 - What is while True: break in Python? while True: break is a loop that starts and immediately exits on the first iteration because break is the first thing executed. In practice this is equivalent to no loop at all - it is sometimes seen in example ...
๐ŸŒ
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 - To do something similar to this example, you would need to make use of Python's while loop. The general syntax for writing a while loop in Python looks like this: while condition: body of while loop containing code that does something ... You start the while loop by using the while keyword. Then, you add a condition which will be a Boolean expression. A Boolean expression is an expression that evaluates to either True or False.
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
Given that we have if/break to get out of the loop, it's possible to get rid of the test at the top of the loop entirely, relying on if/break to end the loop. We do this by writing the loop as while True:... Here is an example that uses while/True to go through the numbers 0..9.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-use-while-true-in-python
How to use while True in Python - GeeksforGeeks
July 23, 2025 - This makes while True loops extremely ... the loop tells you to stop. Example 1: In this example, we use while True to create an infinite loop using the pass statement....
Find elsewhere
๐ŸŒ
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
๐ŸŒ
Readthedocs
pc-microbit-micropython.readthedocs.io โ€บ en โ€บ latest โ€บ lessons โ€บ while_True_loops.html
2. while True loops โ€” PC-Microbit-Micropython
Instead of using a condition that returns True or False, True is used in place of the condition to make the condition always True. As a result, the while-loop runs forever.
๐ŸŒ
YouTube
youtube.com โ€บ watch
While loops in Python are easy!
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Refactoring: "while True" Infinite Loops & The "input" Function - YouTube
https://dbader.org/python-tricks โ–บ Write clean and efficient Python code with this bite-sized series of free & practical Python tips.In this video I'm refact...
Published: June 27, 2017
๐ŸŒ
Bomberbot
bomberbot.com โ€บ python โ€บ while-loops-in-python-while-true-loop-statement-example
While Loops in Python โ€“ While True Loop Statement Example - Bomberbot
April 24, 2024 - The variable count is initialized ... look at a few more examples to demonstrate different use cases for while loops. ... while True: age = input("Enter your age: ") if age.isdigit(): break print("Please enter a valid number.") print(f"Your age is {age}")...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_while_loop.php
C While Loop
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... Loops can execute a block of code as long as a specified condition is true. Loops are handy because they save time, reduce errors, and they make code more readable. The while loop repeats a block of code as long as a specified condition is true:
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_loop_while.asp
JavaScript While Loop
JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Certificate ... While loops execute a block of code as long as a specified condition is true.
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ using the raspberry pi โ€บ troubleshooting
Python "while True:" loop exits without error - Raspberry Pi Forums
B.Goode wrote: Well, it seems that looking at your code that if debugMode is set to be True (which it is) then you should get a timestamp and a load of other information including the loop counter written to your terminal each time the While loop executes. So when the script exits you should still have the last debug message on the screen. Does examining that tell you anything? What happens if counter gets to be a larger number than Python can handle for example?
๐ŸŒ
Kansas State University
textbooks.cs.ksu.edu โ€บ intro-python โ€บ 05-loops โ€บ 02-while-loops
While Loops :: Introduction to Python
July 30, 2026 - Notice that this syntax is very similar to an if statement. We start with the keyword while, followed by some sort of a <boolean expression> that will evaluate to either True or False. The Boolean expression in Python does not need to be placed inside of parentheses.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75851108 โ€บ close-a-while-true-loop-from-another-while-true
python - Close a While True Loop from another While True - Stack Overflow
breaker = False #our mighty loop exiter! while True: while True: if conditionMet: #insert code here... breaker = True break if breaker: # the interesting part!
๐ŸŒ
Medium
harshilbmk.medium.com โ€บ while-loops-in-python-day-11-931b2839cfc4
While Loops in Python โ€” Day 11. Python While Loops | by Harshil Chovatiya | Medium
September 3, 2024 - The condition is evaluated before each iteration of the loop. If the condition is false, the loop is terminated. ... In this example, the loop continues to execute as long as the value of i is less than or equal to 5.