Here’s how to make an infinite loop in Python: write while True:, put your repeating code inside, and use break when it’s time to stop. Without a break, it runs until you press Ctrl+C:
while True:
command = input("> ")
if command == "quit":
break
I typed the answers into a real Command Prompt for the first example, and every loop here ran on Python 3.12.5.
You’ll see a while True menu, endless counters with itertools, retry and polling loops with limits, how to stop a runaway loop, and how to spot an accidental one.
How to make an infinite loop with while True
while True never becomes false on its own, so the loop body repeats until something inside calls break. This cart keeps asking for prices until you type q:
cart = 0.0
while True: # runs until we break out
choice = input("Add item price, or 'q' to check out: ").strip()
if choice.lower() == "q":
break
try:
cart += float(choice)
except ValueError:
print("Please type a number like 4.50")
continue # skip the print below, ask again
print(f"Cart total: ${cart:.2f}")
print(f"Checked out: ${cart:.2f}")
Output:
Add item price, or 'q' to check out: 4.50
Cart total: $4.50
Add item price, or 'q' to check out: abc
Please type a number like 4.50
Add item price, or 'q' to check out: 12.25
Cart total: $16.75
Add item price, or 'q' to check out: q
Checked out: $16.75

continue skips back to the prompt. Typing q runs break.break leaves the loop and continue jumps to the next pass. Both are explained in break and continue in Python.
Infinite loops with itertools.count and cycle
When you want an endless sequence rather than an endless condition, itertools has two handy helpers. count() counts forever and cycle() repeats a list forever:
from itertools import count, cycle
for ticket in count(1001): # 1001, 1002, ... forever
print("ticket", ticket)
if ticket == 1003:
break
shifts = cycle(["Alice", "Ben", "Carla"]) # repeat a list forever
for day in ["Mon", "Tue", "Wed", "Thu", "Fri"]:
print(day, next(shifts))
Output:
ticket 1001
ticket 1002
ticket 1003
Mon Alice
Tue Ben
Wed Carla
Thu Alice
Fri Ben

count(1001) needs a break; cycle() here stops because the weekday list ends.Retry loops: infinite, but with a limit
I use while True most often for retrying a flaky operation. Always add a maximum number of attempts so a permanent failure can’t hang your program:
import random
random.seed(4)
attempt = 0
while True:
attempt += 1
ok = random.random() > 0.7 # pretend this is a flaky network call
print(f"attempt {attempt}: {'success' if ok else 'failed'}")
if ok:
break
if attempt == 5:
print("giving up after 5 attempts")
break
Output:
attempt 1: failed
attempt 2: failed
attempt 3: failed
attempt 4: failed
attempt 5: failed
giving up after 5 attempts
In real code, add a short time.sleep() between attempts. There’s more on loops that check a condition each pass in the Python while loop guide.
A polling loop with a timeout
Background workers and scripts that wait for a job often poll in a loop. Use time.monotonic() for the deadline; unlike the wall clock, it never jumps when the system time changes:
import time
start = time.monotonic()
checks = 0
while True:
checks += 1
status = "done" if checks == 4 else "processing" # stand-in for checking a job
print(f"{time.monotonic() - start:4.1f}s job status: {status}")
if status == "done":
break
if time.monotonic() - start > 10:
print("timed out")
break
time.sleep(0.5)
Output:
0.0s job status: processing
0.5s job status: processing
1.0s job status: processing
1.5s job status: done

How do you stop an infinite loop in Python?
Press Ctrl+C in the terminal. Python raises KeyboardInterrupt, which stops the loop. Catch it if you want to clean up or print a goodbye instead of a traceback:
try:
while True:
process_next_order()
except KeyboardInterrupt:
print("Stopped by user, saving progress...")
In IDLE or a Jupyter notebook, use the stop or interrupt button. If a program ignores Ctrl+C, close the terminal window or end the process from Task Manager.
Spotting an accidental infinite loop
Most accidental infinite loops come from a condition that never turns false: a forgotten increment, or a float that never hits an exact value. A safety cap makes them easy to catch:
x = 0.0
steps = 0
MAX_STEPS = 1_000 # a safety cap while you debug
while x != 1.0: # 0.1 added ten times is 0.9999999999999999, not 1.0
x += 0.1
steps += 1
if steps >= MAX_STEPS:
print(f"stopped by the cap after {steps} steps, x = {x:.1f}")
break
x, steps = 0.0, 0
while x < 1.0 - 1e-9: # compare with a tolerance (or count instead)
x += 0.1
steps += 1
print(f"fixed loop: {steps} steps, x = {x}")
Output:
stopped by the cap after 1000 steps, x = 100.0
fixed loop: 10 steps, x = 0.9999999999999999
Without the cap, the first loop would never end, because 0.1 can’t be stored exactly. Comparing floats with == or != is risky; see the not equal operator for why. If a recursive function loops instead, you’ll get a RecursionError rather than a hang.
Infinite loop patterns at a glance
| Pattern | Stops when |
|---|---|
while True: ... break | A condition inside calls break |
for n in count(): | You break out |
for x in cycle(items): | You break out, or zip it with a finite list |
| Retry with max attempts | Success, or the attempt limit |
| Polling with a deadline | The job finishes, or time runs out |
More loop control topics to read next:
Frequently asked questions
How do I make an infinite loop in Python?
Write while True: and indent the code to repeat. Add a break inside for the exit condition.
How do I stop an infinite loop?
Press Ctrl+C in the terminal, which raises KeyboardInterrupt. In an editor, use its stop button.
Is while True bad practice?
No. It’s a common, readable pattern for menus, servers and retry loops, as long as there’s a clear break condition.
What’s the difference between while True and while 1?
Nothing meaningful in Python 3. while True is clearer, so use that.
How do I make a for loop run forever?
Loop over itertools.count() or itertools.cycle(items), which never run out.
Where is KeyboardInterrupt documented?
See KeyboardInterrupt in the Python docs.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn