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
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
Stopping an iteration without using `break` in Python 3 - Stack Overflow
dictionary - Breaking a loop without ending the program (python) - Stack Overflow
Another way to stop loop without using a break statement
How can we exit a loop?
Videos
You could always use a function and return from it:
import logging
def func():
for i, x in enumerate(x):
logging.info("Processing `x` n.%s...", i)
y = do_something(x)
if y == A:
logging.info("Doing something else...")
do_something_else(x)
elif y == B:
logging.info("Done.")
return # Exit the function and stop the loop in the process.
func()
Although using break is more elegant in my opinion because it makes your intent clearer.
You could use a boolean value to check if you are done. It will still iterate the rest of the loop but not execute the code. When it is done it will continue on its way without a break. Example Pseudo code below.
doneLogging = False
for i, x in enumerate(x):
if not doneLogging:
logging.info("Processing `x` n.%s...", i)
y = do_something(x)
if y == A:
logging.info("Doing something else...")
do_something_else(x)
elif y == B:
logging.info("Done.")
doneLogging = True
Hi! I'm a freshman who just started an intro class in Computer Programming with Python. I've already finished the assignment but I just wanted to know another way to end the loop without using a break statement. I'm not really good with programming, so I apologize if this post is kind of dumb. This is my code:
import random
random.seed()
print("Hello! Welcome to the coolest guessing game ever.")
r = random.randint(1, 100)
while True:
g = input("Guess the number I'm thinking of from 1 to 100. ")
g = int(g)
if g == r:
print("You win!")
break
elif g < r:
print("Try a higher number. :(")
elif g > r:
print("Try a lower number. :(")My code does work, and I am content with how it is, but I do want to learn other ways to end looping without the break statement.