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
🌐
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:
🌐
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.
June 23, 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.
🌐
Mimo
mimo.org › glossary › python › break-statement
Python Break Statement: Syntax, Usage, and Practical Examples
The Python break statement is a valuable tool for controlling loop execution. It allows you to exit for or while loops as soon as a condition is met, making your code cleaner, faster, and easier to understand.
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - So far, you’ve seen examples where the entire loop body runs on each iteration. Python provides two keywords that let you modify that behavior: break: Immediately terminates a loop.
🌐
Medium
sibabalwesinyaniso.medium.com › mastering-while-loops-break-and-continue-in-python-controlling-program-flow-efficiently-e3a7512b3710
Mastering While Loops, Break, and Continue in Python — Controlling Program Flow Efficiently | by Sibabalwe Sinyaniso | Medium
February 4, 2025 - The loop runs indefinitely until the user enters "exit", triggering break. The continue statement skips the current iteration and moves to the next one. num = 0 while num < 10: num += 1 if num % 2 == 0: continue print(f"Odd number: {num}")
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
🌐
Programiz
programiz.com › python-programming › break-continue
Python break and continue (With Examples)
In the above example, we have used the while loop to print the odd numbers between 1 and 10. Here, if (num % 2) == 0: continue · skips the current iteration when the number is even and starts the next iteration. Also Read: Python pass Statement · Python range() Introduction · Python break Statement ·
🌐
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.
Published   June 5, 2026
🌐
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 else clause on for and while loops executes only when the loop completes normally without hitting a break, making it ideal for implementing clear and concise “not found” logic. You should have Python 3 installed and a programming environment on your computer or server.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.6 documentation
We have seen that the for statement ... Data Structures, we will discuss list() in more detail. The break statement breaks out of the innermost enclosing for or while loop:...
🌐
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 ...
🌐
Quora
quora.com › How-do-I-break-a-loop-in-Python-without-using-data-break-continue-def-or-others
How to break a loop in Python without using data, break, continue, def, or others - Quora
Answer: You can use exception handling to break a loop. For example: [code]class BreakLoop(Exception): pass try: while True: # Your code here raise BreakLoop except BreakLoop: pass [/code]This way, you can exit the loop without directly using [code ]break[/code].
🌐
Python Forum
python-forum.io › thread-21902.html
Do break operators turn while loop conditions from True to False?
Check out this very basic while loop in Python: while True: response = input('Say something: ') if response == 'bye': break if response != 'bye': continue This script prompts the user for input. If the user would like ...
🌐
W3Schools
w3schools.com › python › ref_keyword_break.asp
Python break Keyword
Python Examples Python Compiler ... Python Interview Q&A Python Bootcamp Python Training ... The break keyword is used to break out a for loop, or a while loop....
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - You can skip the current iteration and move to the next one using the continue statement. break terminates the entire while loop, whereas continue only skips the remaining code in the current iteration.
🌐
Quora
quora.com › In-Python-why-does-the-break-statement-work-inside-of-a-while-loop-but-not-an-if-statement
In Python, why does the 'break' statement work inside of a while loop but not an if statement? - Quora
Answer (1 of 2): A loop basically uses a jump to go back to the beginning of its code block. Some inherently mutate variables along the while while others leave that part to be done in the aforementioned code block itself. Consider how else you could terminate the loop similar to the break keywor...
🌐
Python.org
discuss.python.org › python help
The while loop isn't breaking - Python Help - Discussions on Python.org
October 15, 2023 - While true : D = input(‘enter 0 to exit’) If D == 0: Break Print(‘loop ended’) This loop isn’t breaking and is asking ‘enter 0 to exit’ again and again. Is there something wrong with the code.
🌐
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.