How to Repeat Code Until You Stop It in Python with while True and break
Some loops don't know in advance how many times to run: a chat, a menu, a prompt that repeats until the user is done. while True runs the body over and over, and break leaves the loop the moment you decide. This guide loops until the user types quit, with real output from Python 3.15.2.
Syntax
while True:
<code>
if <condition>:
break
<more code>
| Part | What it means |
|---|---|
while True: | Runs the indented body again and again; True never stops it by itself |
if <condition>: break | Leaves the loop immediately when the condition holds |
| After the loop | The code below the loop runs once break has run |
Python's language reference: "The while statement is used for repeated execution as long as an expression is true". About break, it says: "It terminates the nearest enclosing loop".
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
while True:
answer = input("Type something, or quit to stop: ")
if answer == "quit":
break
print(f"You typed {answer}")
print("Bye")

The check comes right after reading the answer, so quit itself is never echoed.
Step 2: Run It
uv run python repeat.py
Type hello, press Enter, then type quit:
Type something, or quit to stop: hello
You typed hello
Type something, or quit to stop: quit
Bye

The loop ran twice. On the second turn break ended it, and Bye, the first line after the loop, ran.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Forgetting the break | The loop never ends; stop it with Ctrl+C | Give every while True a way out |
Putting break outside a loop | SyntaxError: 'break' outside loop | Use break only inside for or while |
| Checking the condition after printing | The stop word is echoed too | Check first, then act |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Typing quit doesn't stop it | A trailing space or capital letters, such as Quit | Compare answer.strip().lower() |
FAQ
Can I write while answer != "quit": instead?
Yes, but then answer needs a value before the loop starts. while True with break reads the input in one place, at the top of the loop.
What does break do in nested loops?
It leaves only the innermost loop it's in.
Conclusion
while True kept asking until the answer was quit, break ended the loop on the second turn, and Bye printed after it.
Additional Resources
- The while statement — repeated execution.
- The break statement — leaving a loop.
