38

I have to make this game for my comp class, and I can't figure out how how break out of this loop. See, I have to play against the "computer," by rolling bigger numbers, and seeing who has the bigger score. But I can't figure out how to "break" from my turn, and transition to the computers turn. I need "Q" (quit) to signal the beginning of the computers turn, but I don't know how to do it.

ans=(R)
while True:
    print('Your score is so far '+str(myScore)+'.')
    print("Would you like to roll or quit?")
    ans=input("Roll...")
    if ans=='R':
        R=random.randint(1, 8)
        print("You rolled a "+str(R)+".")
        myScore=R+myScore
    if ans=='Q':
        print("Now I'll see if I can break your score...")
        break
Belphegor
  • 4,456
  • 11
  • 34
  • 59
Ninja
  • 379
  • 1
  • 3
  • 4
  • Using `break` the way you are is fine, but you have to type exactly `Q`. `q` won't work for example. Is the first line supposed to say `ans=('R')`? you don't need it anyway – John La Rooy Jan 30 '13 at 00:28

5 Answers5

23

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
SabreWolfy
  • 5,392
  • 11
  • 50
  • 73
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 2
    Please correct me if needed- break sends a False signal to stop the while loop? – Learner Oct 23 '15 at 20:25
  • 6
    @SIslam Kind of. `break` stops the `while` loop, but there isn't a 'False signal': `while` means 'loop while the expression following the `while` statement evaluates as True', so if what comes after `while` is `True` itself, `while` will loop forever; `break` means 'stop looping right now' and works any loop, including both `while` and `for` loops. – Westcroft_to_Apse Aug 21 '16 at 20:01
12

What I would do is run the loop until the ans is Q

ans=(R)
while not ans=='Q':
    print('Your score is so far '+str(myScore)+'.')
    print("Would you like to roll or quit?")
    ans=input("Roll...")
    if ans=='R':
        R=random.randint(1, 8)
        print("You rolled a "+str(R)+".")
        myScore=R+myScore
Phil
  • 6,686
  • 2
  • 19
  • 25
9

Don't use while True and break statements. It's bad programming.

Imagine you come to debug someone else's code and you see a while True on line 1 and then have to trawl your way through another 200 lines of code with 15 break statements in it, having to read umpteen lines of code for each one to work out what actually causes it to get to the break. You'd want to kill them...a lot.

The condition that causes a while loop to stop iterating should always be clear from the while loop line of code itself without having to look elsewhere.

Phil has the "correct" solution, as it has a clear end condition right there in the while loop statement itself.

Steve Todd
  • 107
  • 1
  • 1
4
ans=(R)
while True:
    print('Your score is so far '+str(myScore)+'.')
    print("Would you like to roll or quit?")
    ans=input("Roll...")
    if ans=='R':
        R=random.randint(1, 8)
        print("You rolled a "+str(R)+".")
        myScore=R+myScore
    else:
        print("Now I'll see if I can break your score...")
        ans = False
        break
aug2uag
  • 3,379
  • 3
  • 32
  • 53
0

Walrus operator (assignment expressions added to python 3.8) and while-loop-else-clause can do it more pythonic:

myScore = 0
while ans := input("Roll...").lower() == "r":
    # ... do something
else:
    print("Now I'll see if I can break your score...")
DRPK
  • 2,023
  • 1
  • 14
  • 27