Search code examples
pythonloopsif-statementwhile-loop

While True loop in python does not stop even when condition true is not met


This is a simple rock, paper or scissors game. Whenever any of the players get a score of 3, the loop should break and print "game over". I'm a beginner but i cannot understand why the while True loop keeps running even when either score1 or score2 are >3.

print("---Rock 🪨 paper 📄 or scissors ✂---")
print("")
print("Choose your move; R, P or S")

score1=0
score2=0
while True:
  player_1 = input ("Player 1 move? ")
  player_2 = input ("Player 2 move? ")
  if score1 <3 or score2 <3:
    if player_1 == player_2:
      print("Even round")
      continue
    elif player_1 == "P" and player_2 == "R":
      print ("Player 1 won!")
      score1 += 1
      print(score1)
      continue
    elif player_1 == "P" and player_2 == "S":
      print ("Player 1 won!")
      score1 += 1
      print(score1)
      continue
    elif player_1 == "R" and player_2 == "P":
      print ("Player 1 won!")
      score1 += 1
      print(score1)
      continue
    elif player_1 == "R" and player_2 == "S":
      print ("Player 1 won!")
      score1 += 1
      print(score1)
      continue
    elif player_1 == "S" and player_2 == "R":
      print("Player 2 won!")
      score2 += 1
      print(score2)
      continue
    elif player_1 == "S" and player_2 == "P":
      print("Player 2 won!")
      score2 += 1
      print(score2)
      continue
  else:
    print("Game over")
    break
  
print ("Player 1 score was =", score1)
print ("Player 2 score was =", score2)

I ran out of solutions.


Solution

  • I think there are a few issues with this code:

    1. The first is the condition, score1 <3 or score2 <3. If you want the code to stop once any of the players reaches 3, you should change the or to and. Having an or means that either of them is true, then executes the code, so even when let's say score1 has been updated to 3 the loop continues.
    2. You should switch your input after the conditioning, to make sure that you do not take input despite when your exit criteria are reached.

    And one bonus; Instead of having an infinite loop and continues while True: you can use condition the while itself: while score1 < 3 and score2 < 3:

    Also, if player1 is R and player2 is P; Shouldn't the player2 win?

    One modified version would be:

    print("---Rock 🪨 paper 📄 or scissors ✂---")
    print("")
    print("Choose your move; R, P or S")
    
    score1=0
    score2=0
    while score1 < 3 and score2 < 3:
        print(f"Current scores: {score1} - {score2}")
        player_1 = input ("Player 1 move? ")
        player_2 = input ("Player 2 move? ")
        if player_1 == player_2:
            print("Even round")
        elif player_1 == "P" and player_2 == "R":
            print ("Player 1 won!")
            score1 += 1
        elif player_1 == "P" and player_2 == "S":
            print ("Player 1 won!")
            score1 += 1
        elif player_1 == "R" and player_2 == "P":
            print ("Player 2 won!")
            score2 += 1
        elif player_1 == "R" and player_2 == "S":
            print ("Player 1 won!")
            score1 += 1
        elif player_1 == "S" and player_2 == "R":
            print("Player 2 won!")
            score2 += 1
        elif player_1 == "S" and player_2 == "P":
            print("Player 2 won!")
            score2 += 1
    
    print("Game over")
      
    print ("Player 1 score was =", score1)
    print ("Player 2 score was =", score2)