You can use break to get out of a loop. Your code also has some other errors, which I've fixedd. Try this:
full_name = input ('Enter a customers name (or q to quit): ')
if full_name == 'q':
exit()
address = input ("Enter the customer's address: ")
location = input("Enter customer's city, state, and zip code: " )
text = ''
while text.lower() != 'q':
thin_M = int(input('Enter the number of Thin Mints: '))
if thin_M > 10:
print ('Sorry we are accepting a maximum of 10 boxes per type.')
else:
break
Answer from Michael M. on Stack Overflow Top answer 1 of 2
1
You can use break to get out of a loop. Your code also has some other errors, which I've fixedd. Try this:
full_name = input ('Enter a customers name (or q to quit): ')
if full_name == 'q':
exit()
address = input ("Enter the customer's address: ")
location = input("Enter customer's city, state, and zip code: " )
text = ''
while text.lower() != 'q':
thin_M = int(input('Enter the number of Thin Mints: '))
if thin_M > 10:
print ('Sorry we are accepting a maximum of 10 boxes per type.')
else:
break
2 of 2
0
First, you never change cancel variable so the condition is always True and it then loops forever. I assume you rather wanted to check full_name against q as per your input prompt.
Also thin_M = 'Thin Mints' assignment is pointless as it gets overwritten before any read, so just remove it.
And finally, 2nd while should rather be plain if.
full_name = input ('Enter a customers name (or q to quit): ')
address = input ("Enter the customer's address: ")
location = input("Enter customer's city, state, and zip code: " )
cancel = full_name.lower() == 'q'
while cancel == False:
print ()
thin_M = int(input('Enter the number of Thin Mints: '))
if thin_M > 10:
print ('Sorry we are accepting a maximum of 10 boxes per type.')
else:
cancel = True
W3Schools
w3schools.com › Python › python_for_loops.asp
Python For Loops
The for loop does not require an indexing variable to set beforehand. Even strings are iterable objects, they contain a sequence of characters: ... fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break Try it Yourself » · Exit the loop when x is "banana", ...
How does python know where the end of a for-loop code block is? There's no indication of when execution should restart.
Mark Miller is having issues with: I can't use "continue" or "break" because the code block should execute for each item, and then break when no further items are available. The c... More on teamtreehouse.com
Breaking/continuing out of multiple loops - Ideas - Discussions on Python.org
Hi! SUGGESTION An easy way to break/continue within nested loops. MY REAL LIFE EXAMPLE I am looping through a list of basketball players, each of which has multiple tables, each of which can have up to three different versions. I manipulate data in the deepest loop and, if some key data is ... More on discuss.python.org
How can I exit a loop in Python when a certain condition is met? - Ask a Question - TestMu AI Community
How can I exit a loop in Python when a certain condition is met? I have a loop, and I want it to stop when x + y = z. Here’s my code: from random import randint import os x = randint(1, 11) y = randint(1, 11) print("… More on community.testmuai.com
How do I end a For or While loop without using break?
A for loop ends when it exhausts its iterable, and a while loop terminates when its condition is no longer met. If you were going to write while True: if some_variable == "some value": break #do stuff then instead you could write: while some_variable != "some value": #do stuff More on reddit.com
Videos
05:16
Python Break Statement Explained | Exit Loops Like a Pro! - YouTube
02:43
Breaking out of a loop in Python - YouTube
Break Out of Loops With Python's break Keyword: Getting to ...
05:32
How to exit a for loop in Python ? - YouTube
02:26
Exit a loop on Condition in Python - YouTube
05:32
Exit for loop in Python | Break and Continue statements - YouTube
Top answer 1 of 4
6
A for loop ends when it exhausts its iterable, and a while loop terminates when its condition is no longer met. If you were going to write while True: if some_variable == "some value": break #do stuff then instead you could write: while some_variable != "some value": #do stuff
2 of 4
1
See itertools takewhile and dropwhile.
Enki
enki.com › post › how-to-exit-a-loop-in-python
Enki | Blog - How to Exit a Loop in Python
Learn to manage loops effectively in Python using the break statement to exit for and while loops under specific conditions, optimizing code efficiency.
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How To Use break, continue, and pass Statements in Python | DigitalOcean
April 24, 2026 - The break statement allows you to exit a loop entirely when a specific condition is met, effectively stopping the loop execution. The continue statement lets you skip the rest of the code inside the loop for the current iteration and move on to the next iteration.
Mimo
mimo.org › glossary › python › break-statement
Mimo: The coding platform you need to learn Web Development, Python, and more.
This simple yet powerful control structure is especially useful for optimizing loop behavior, avoiding unnecessary iterations, and writing more efficient and readable code. The break statement in Python is used to immediately exit a loop—either a for loop or a while loop—before the loop ...
Facebook
facebook.com › groups › selftaughtprogrammers › posts › 1347051345658596
How to exit a Python program from an inner loop
We cannot provide a description for this page right now
Python Software Foundation
wiki.python.org › python › ForLoop.html
For loops - Python Wiki
Like the while loop, the for loop can be made to exit before the given object is finished. This is done using the break statement, which will immediately drop out of the loop and continue execution at the first statement after the block.
GeeksforGeeks
geeksforgeeks.org › python › python-break-statement
Python Break Statement - GeeksforGeeks
The break statement in Python is used to immediately terminate a for or while loop when a specified condition is met. After the loop exits, program execution continues with the next statement following the loop.
Published June 5, 2026
Manifoldapp
cuny.manifoldapp.org › read › how-to-code-in-python-3 › section › 8adf4bec-a6ca-4a11-8c8d-4cf4052d5ac4
How To Use Break, Continue, and Pass Statements when Working with Loops | How To Code in Python 3 | Manifold @CUNY
In Python, the break statement provides you with the opportunity to exit out of a loop when an external condition is triggered. You’ll put the break statement within the block of code under your loop statement, usually after a conditional if statement. Let’s look at an example that uses ...
GeeksforGeeks
geeksforgeeks.org › python › break-continue-and-pass-in-python
Loop Control Statements - Python
July 12, 2025 - # Using For Loop for i in range(5): if i == 3: break # Exit the loop when i is 3 print(i) # Using While Loop i = 0 while i < 5: if i == 3: break # Exit the loop when i is 3 print(i) i += 1 ... Python Continue statement is a loop control statement that forces to execute the next iteration of the loop while skipping the rest of the code inside the loop for the current iteration only, i.e.
Python.org
discuss.python.org › ideas
Breaking/continuing out of multiple loops - Ideas - Discussions on Python.org
August 21, 2022 - Hi! SUGGESTION An easy way to break/continue within nested loops. MY REAL LIFE EXAMPLE I am looping through a list of basketball players, each of which has multiple tables, each of which can have up to three different versions. I manipulate data in the deepest loop and, if some key data is ...
Python
wiki.python.org › moin › ForLoop
ForLoop
Like the while loop, the for loop can be made to exit before the given object is finished. This is done using the break statement, which will immediately drop out of the loop and continue execution at the first statement after the block.
TestMu AI Community
community.testmuai.com › ask a question
How can I exit a loop in Python when a certain condition is met? - Ask a Question - TestMu AI Community
December 2, 2024 - How can I exit a loop in Python when a certain condition is met? I have a loop, and I want it to stop when x + y = z. Here’s my code: from random import randint import os x = randint(1, 11) y = randint(1, 11) print("", x, "+", y, "=\n") z = int(input("Resposta=")) if z == x + y: input("\n\nCorreto\nPrima enter para continuar...") else: for z in range(0, 10): os.system('CLS') input("Incorreto.\n\n Tente de novo...") x = randint(1, 11) y = randint(1, 1...
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.6 documentation
In either kind of loop, the else clause is not executed if the loop was terminated by a break. Of course, other ways of ending the loop early, such as a return or a raised exception, will also skip execution of the else clause. This is exemplified in the following for loop, which searches for prime numbers:
Programiz
programiz.com › python-programming › break-continue
Python break and continue (With Examples)
The break statement terminates the loop immediately when it's encountered. ... The above image shows the working of break statements in for and while loops.