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:
Discussions

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
🌐 discuss.python.org
4
August 21, 2022
The while loop isn't breaking
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. More on discuss.python.org
🌐 discuss.python.org
0
0
October 15, 2023
how to stop the while loop
Andrew McLane is having issues with: So I added a while loop so that the loop could run for words that had multiple vowels, and multiple of the same vowels. However, I can't figure ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
October 29, 2017
Infinte while loop using try/except
hey y’all! I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure. I’m running into an infinite loop when the exceptions are raised in the convert() function. More on discuss.python.org
🌐 discuss.python.org
0
0
June 15, 2024
🌐
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

🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-break-statement
Python break statement - GeeksforGeeks
The break statement can be used within a for loop to exit the loop before it has iterated over all items, based on a specified condition. ... A while loop in Python repeatedly executes a block of code as long as a specified condition is True.
Published   2 weeks ago
Find elsewhere
🌐
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 - Learn how to break out of nested loops in Python, exit specific loop levels using flags, functions, exceptions, and best practices for clean loop control
🌐
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.
🌐
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.
🌐
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
It’s easy to think here that these two breaks on lines 4 and 5 would first, stop the for loop and then stop the while loop right? This is incorrect, once you break, the execution of a loop immediately stops. This means that the break on line 5 is never used. I hope this helped! Don’t forget there is always help when you need it, if you have any more questions or are looking for a structured way to learn Python online, I have created a four-stage course that teaches the fundamentals of Python.
🌐
W3Schools
w3schools.com › python › ref_keyword_break.asp
Python break Keyword
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate 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.
🌐
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 th
🌐
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.
🌐
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!")
🌐
Team Treehouse
teamtreehouse.com › community › how-to-stop-the-while-loop
how to stop the while loop (Example) | Treehouse Community
October 29, 2017 - def disemvowel(word): while True: ... except ValueError: pass return word ... The python 'break' statement is used to break out of a loop....
🌐
Python.org
discuss.python.org › python help
Infinte while loop using try/except - Python Help - Discussions on Python.org
June 15, 2024 - hey y’all! I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure. I’m running into an infinite loop when the exceptions a…