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: ") Answer from mopslik on reddit.com
🌐
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: ")
Discussions

Infinte while loop using try/except
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 are raised in the convert() function. More on discuss.python.org
🌐 discuss.python.org
0
June 15, 2024
programming practices - while(true) and loop-breaking - anti-pattern? - Software Engineering Stack Exchange
Loops can become very involved and one or more clean breaks can be a lot easier on you, anyone else looking at your code, the optimizer, and the program's performance than elaborate if-statements and added variables. My usual approach is to set up a loop with something like the "while (true)" and ... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
March 29, 2012
Python : While True or False - Stack Overflow
What do you expect the True and False to do here? They are just references to the boolean values on a line, they are otherwise discarded here. ... @MartijnPieters I expect, if 'False' , it quits the while loop and the script continue to my other loop which I didn't include More on stackoverflow.com
🌐 stackoverflow.com
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
April 30, 2022
🌐
Replit
replit.com › home › discover › how to use 'while true' in python
How to use 'while True' in Python | Replit
2 weeks 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
🌐
Readthedocs
pc-microbit-micropython.readthedocs.io › en › latest › lessons › while_True_loops.html
2. while True loops — PC-Microbit-Micropython
2. while True loops · Previous Next · See: `<https://www.w3schools.com/python/python_while_loops.asp · While loops run a set of statements as long as a test condition is true. while True: loops run forever. Instead of using a condition that returns True or False, True is used in place of ...
🌐
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 are raised in the convert() function.
🌐
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 - 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.
🌐
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.
Find elsewhere
🌐
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 documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
For more on the try statement and exceptions, see Handling Exceptions. 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: ...
🌐
Server Academy
serveracademy.com › blog › python-while-loop-tutorial
Python While Loop Tutorial - Server Academy
November 12, 2024 - While infinite loops can be useful, they should be used with caution to avoid unintentional infinite runs. while True: print("This will run forever unless stopped manually.") This code will continue printing indefinitely until you manually interrupt ...
🌐
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....
🌐
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 ...
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
If we want a block of code to execute ... with the condition "True", which means that the loop will run infinitely until we break out of it using "break" keyword or some other logic....
Published   1 month ago
🌐
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……
🌐
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.
🌐
Board Infinity
boardinfinity.com › blog › use-while-true-in-python
Use While True in Python | Board Infinity
August 13, 2025 - In conclusion, the "while True" loop is a useful tool in Python programming that allows a block of code to be repeated indefinitely. It is often used with a break statement to exit the loop under certain conditions.