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
🌐
Ai-inter1
ai-inter1.com › home › python基本編 › 12. 繰り返し処理(while文)
図解!Python whileループからbreakで抜ける方法を徹底解説! - ビジPy
February 3, 2026 - while Trueとwhileの条件文にTrueを記述すると、条件が常にTrueになり、無限に繰り返し処理が続くことになります。そこで、実行したい処理に続けてif文とbreakを記述します。すると、if文の条件判定の前に処理があるので、必ず1回は実行されることになります。 · 例えば、次のようなwhile文があるとします。 ... ここでは、変数aが0から2まで処理が繰り返されています。ここで使われているprint、formatの詳しい説明は、「PythonにおけるPrint、Formatの使い方」を参照ください。
🌐
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.
🌐
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.
🌐
Python
python.jp › train › loop › break-statement.html
break文による中断: ゼロからのPython入門講座 - python.jp
counter = 1 # 現在、何回目かを記録する変数 while counter <= 5: # counterの値が5以下なら繰り返す text = input("数字を入力してください") # 入力された文字が '999' なら if text == '999': # ループを中断する print("中断します") break number = int(text) # 入力した文字列を数値に変換する print(counter, "回目:", number * number) # 入力した数値の2乗を表示する counter = counter + 1 # counter の値に 1 を加算する print("終了しました")
🌐
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.
🌐
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.
Find elsewhere
🌐
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:
🌐
Tutorialspoint
tutorialspoint.com › python › python_break_statement.htm
Python - break Statement
for letter in 'Python': if letter ... Good bye! Similar to the for loop, we can use the break statement to skip the code inside while loop after the specified condition becomes TRUE....
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_control.htm
Python Break, Continue and Pass Statements
August 18, 2023 - 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-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
🌐
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.
January 31, 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

🌐
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   1 week ago
🌐
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
🌐
Javadrive
javadrive.jp › home › python入門 › pythonにおける繰り返し処理の使い方
Python | break文を使った繰り返し処理の強制終了
September 11, 2024 - while 文であれば条件式が真の間繰り返しブロック内の処理を実行しますが、ブロック内で if 文を記述し、 if 文の条件式が真になった時は break を実行するように記述します。 break ...
🌐
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.
🌐
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....
🌐
Pasona
x-tech.pasona.co.jp › トップ › スキルアップ › 【python入門】whileで書くループ処理 - サンプルを元に徹底解説
【Python入門】whileで書くループ処理 - サンプルを元に徹底解説 | Engineer Labo エンジニアラボ
次のサンプルコードでは、dataという配列の値を出力し、値が6以上の場合はbreakでループを抜ける処理を行ないます。 · ここでは、配列dataの5番目の値が6のため、出力は5,2,1,4,6となります。 · data = [5, 2, 1, 4, 6, 3] i = 0 while data[i] != 0: print(data[i]) i += 1 if i >= 5: break
🌐
LabEx
labex.io › tutorials › python-how-to-use-break-statement-to-exit-a-while-loop-in-python-397695
How to use break statement to exit a while loop in Python | LabEx
By understanding the basic structure and usage of while loops in Python, you'll be able to use them effectively in your programming tasks. The break statement in Python is used to exit a loop prematurely, even if the loop's condition is still True.
🌐
Coursera
coursera.org › tutorials › python-while-loop
How to Write and Use Python While Loops | Coursera
December 23, 2025 - A Python while loop only runs when the condition is met. Since it checks that condition at the beginning, it may never run at all. To modify a while loop into a do while loop, add true after the keyword while so that the condition is always true to begin with. Then, you can add if statements to check the inside condition and break statements to control the flow of the loop based on your terms.