A couple of changes mean that only an R or r will roll. Any other character will quit

import random

while True:
    print('Your score so far is {}.'.format(myScore))
    print("Would you like to roll or quit?")
    ans = input("Roll...")
    if ans.lower() == 'r':
        R = np.random.randint(1, 8)
        print("You rolled a {}.".format(R))
        myScore = R + myScore
    else:
        print("Now I'll see if I can break your score...")
        break
Answer from John La Rooy on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - The input is then converted into an integer number using int(). If the user enters a number thatโ€™s 0 or lower, then the break statement runs, and the loop terminates. Note that to emulate a do-while loop in Python, the condition that terminates the loop goes at the end of the loop, and its body is a break statement.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1. With the break statement we can stop the loop even if the while condition is true:
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ break-continue
Python break and continue (With Examples)
We can use the break statement with the for loop to terminate the loop when a certain condition is met. For example, ... Note: We can also terminate the while loop using a break statement.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i break out of a while loop immediately and at any point during the while loop.
r/learnpython on Reddit: How can I break out of a while loop immediately and at any point during the while loop.
February 8, 2023 -

I have a long while loop, it has a lot of code in it and a lot of delays.

I want to break out of the loop when a condition is met but I need to break out of it instantly. The only thing I can think of, is to print if statements constantly through the loop, checking if the condition is true, but this seems a little silly because it would have literally 100s of break statements.

Is there any better way to do this without typing break statements after every single line of code, that seems impractical because there's 100s of lines of code in the while loop, it takes around 10 minutes to finish and I need it to break instantly

๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-break
Python break statement: break for loops and while loops โ€“ LearnDataSci
Can you see why? while True: res = input("Enter the number 5: ") try: res = int(res) except: pass if res == 5: print("Thanks!") break ... It can be hard to spot when one of your background processes gets caught in an infinite loop. You're not breaking any of Python's rules by getting stuck in a loop, so there are often not any helpful error messages to let you know what you've done wrong.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How to Break Out of Multiple Loops in Python | DigitalOcean
August 7, 2025 - Within the for loop, an if statement presents the condition that if the variable number is equivalent to the integer 5, then the loop will break. You can refer to this tutorial on Using for() loop in Python to learn more about using the for loop. Note: You can also refer to this tutorial on ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-do-while
Python Do While Loops - GeeksforGeeks
July 23, 2025 - Printing list items using while loop geeksforgeeks C++ Java Python C MachineLearning Printing list items using do while loop geeksforgeeks C++ Java Python C ... total = 0 # loop will run at least once while True: # ask the user to enter a number num = int(input("Enter a number (or 0 to exit): ")) # exit the loop if the user enters 0 if num == 0: break total += num # print the total print("Total:", total)
Find elsewhere
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-break
How to Use Python Break | Coursera
When working with loops, remember that indentation tells Python which statements are inside the loop and which are outside the loop. Your break statement should follow your if statement and be indented. ... 1 2 3 4 5 6 7 8 9 10 # break statement for for loop for item in iterable: if some_condition: break # exit the loop # break statement for while loop while condition: # code block if some_condition: break # exit the loop
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ controlflow.html
4. More Control Flow Tools โ€” Python 3.14.3 documentation
In a for loop, the else clause is executed after the loop finishes its final iteration, that is, if no break occurred. In a while loop, itโ€™s executed after the loopโ€™s condition becomes false.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-break-statement
Python break statement - GeeksforGeeks
The break statement in Python is used to exit or "break" out of a loop (either for or while loop) prematurely, before the loop has iterated through all its items or reached its condition.
Published ย  July 12, 2025
๐ŸŒ
Texas Instruments
education.ti.com โ€บ en โ€บ resources โ€บ computer-science-foundations โ€บ do-while-loops
Python coding: Do While Loops | Texas Instruments
An If conditional statement houses this break command to end the loop. The condition on the If statement determines whether the loop will run again or whether the loop will exit. Note that this loop structure is slightly different than Do While ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_loop_control.htm
Python Break, Continue and Pass Statements
for letter in 'Python': # First Example if letter == 'h': break print ('Current Letter :', letter) var = 10 # Second Example while var > 0: print ('Current variable value :', var) var = var -1 if var == 5: break print ("Good bye!")
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-while-loop
How to Write and Use Python While Loops | Coursera
This loop will print the numbers 1 to 5, which is equivalent to the behavior of a do while loop that iterates from 1 to 5. We use a break statement to exit the loop once we have printed all the numbers, since the while condition is always True ...
๐ŸŒ
Hackr
hackr.io โ€บ home โ€บ articles โ€บ programming โ€บ python
Python Do While | Docs With Examples
February 12, 2025 - // Example in C int x = 0; do { printf("x is: %d\n", x); x++; } while (x < 5); This guarantees the loop executes at least once, even if the condition is False at the start. In Python, you can achieve similar behavior using a while loop with a break condition inside:
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ WhileLoop
While loops - Python Wiki
while True: n = raw_input("Please enter 'hello':") if n.strip() == 'hello': break
๐ŸŒ
Python
peps.python.org โ€บ pep-0315
PEP 315 โ€“ Enhanced While Loop | peps.python.org
In the do-while loop the break statement will behave the same as in the standard while loop: It will immediately terminate the loop without evaluating the loop condition or executing the else clause.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-break-and-python-continue-how-to-skip-to-the-next-function
Python Break and Python Continue โ€“ How to Skip to the Next Function
March 14, 2022 - As you can see, the number 50 is not printed to the console because of the continue statement inside the if statement. The break and continue statements in Python are used to skip parts of the current loop or break out of the loop completely.
๐ŸŒ
The Knowledge Academy
theknowledgeacademy.com โ€บ blog โ€บ python-while-loop
Python While Loop: Everything You Should Know
The loop runs forever using while True, but includes a break statement to exit when the user types "exit". Want to master PHP Programming and build dynamic websites? Join our PHP Course now!
๐ŸŒ
Stanford CS
cs.stanford.edu โ€บ people โ€บ nick โ€บ py โ€บ python-while.html
While Loop
i = 0 while i < 10: print(i) if i == 6: break i = i + 1 print('All done') 0 1 2 3 4 5 6 All done