How to break out of while loop in Python?
A couple of changes mean that only an R
or r
will roll. Any other character will quit
import randomwhile 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
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
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