1

I'm making a rock paper scissors game in python from a tutorial book. I think I'm following the book to the letter but for some reason this 'break' statement isn't working.

while True:  # Main game loop
    print('%s Wins, %s Losses, %s Ties' %(wins,losses,ties))
    while True: # Player input loop
        print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit')
        playerMove = input()
        if playerMove== 'q':
            sys.exit() # Quit the program
        if playerMove == 'r' or playerMove =='p' or playerMove =='s':
            print('at least this is working')
            break # Break out of player input loop
        print('but this is not')

        # Display player move
        if playerMove == 'r':
            print('ROCK versus...')
        elif playerMove == 'p':

The code goes on but this is all that is relevant for this problem. When I run it it shows up like this

ROCK,PAPER,SCISSORS
0 Wins, 0 Losses, 0 Ties
Enter your move: (r)ock (p)aper (s)cissors or (q)uit
r
at least this is working
0 Wins, 0 Losses, 0 Ties
Enter your move: (r)ock (p)aper (s)cissors or (q)uit

The 'q' for quit option works just fine so it's clearly getting the input. Apart from that it just keeps repeating the loop. As you can see I put some text in there just to experiment and show where things are breaking down.

What am I doing wrong here?

4 Answers4

3

The break statement will take you out of the innermost loop, not the "main" outer loop. Better change the inner loop to something like this:

input_loop = True
while input_loop: # Player input loop
    print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit')
    playerMove = input()
    if playerMove== 'q':
        sys.exit() # Quit the program
    if playerMove == 'r' or playerMove =='p' or playerMove =='s':
        print('at least this is working')
        input_loop = False # Break out of player input loop
    print('but this is not')
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

You have two nested loops. If you break out of the first one, you immediately re-enter it since you are not getting out of the second loop. I would instead change the first loop to instead of saying while True, say while playing, and set playing to False when game ends.

JeroSquartini
  • 347
  • 1
  • 2
  • 11
2

As others have already told you, you have nested two loops. The break statement only gets you out of the inner loop, but you want to break out of the outer loop. A lot of answers on how to solve this can be found here.

dranjohn
  • 673
  • 7
  • 22
0

I have now resolved this,

initially the reason that

        print('but this is not')

did not print is because I had already broken that while loop.

The reason that the the code below did not run starting with the # Display player move section is because of the indentation. It, and the rest of the program is at the same indentation level as # Player input loop and so was disregarded once that loop was broken.

I moved the rest of the code over one indentation level and program works fine now