Python 3 - while循环满足要求时不跳出循环

Python 3 - While loop not breaking out of the loop when the requirements are met

我的简单石头剪刀布代码:

def game(item1,item2,score1,score2):
   while score1 <= 3 or score2 <= 3 :
          item1 = input("Rock,Paper or Scissors?(1) ")
          item2 = input("Rock,Paper or Scissors?(2) ")
          if item1 == item2:
                 print("It´s a tie! ")
          elif item1 == "Rock":
                 if item2 == "Scissors":
                        score1 +=1
                        print("Player1 wins and has a score of",score1)
                 else:
                        score2 +=1
                        print("Player2 wins and has a score of",score2)
          elif item1 == "Scissors":
                 if item2 == "Paper":
                        score1 +=1
                        print("Player1 wins and has a score of",score1)
                 else:
                        score2 +=1
                        print("Player2 wins and has a score of",score2)
          elif item1 == "Paper":
                 if item2 == "Rock":
                        score1 +=1
                        print("Player1 wins and has a score of",score1)
                 else:
                        score2 +=1
                        print("Player2 wins and has a score of",score2)
          else:
                 print("Invalid input, try again")

当我尝试并达到 3 分时,它仍然不断要求我输入,只有当我在 score1 和 score2 中都达到 3 分时它才会打破循环,这是为什么?我使用 "or" 运算符错了吗?

score1score2 小于等于 3 时,将一直为真,直到两个玩家的分数都高于 3。

正确的条件是只要双方得分都小于3就继续进行。当其中一方的得分变为3时游戏结束

def game():
   score1, score2 = 0, 0
   while score1 < 3 and score2 < 3:  # while neither have won
          item1 = input("Rock,Paper or Scissors?(1) ")
          item2 = input("Rock,Paper or Scissors?(2) ")
          if item1 == item2:
                 print("It´s a tie! ")
          elif item1 == "Rock":
                 if item2 == "Scissors":
                        score1 +=1
                        print("Player1 wins and has a score of",score1)
                 else:
                        score2 +=1
                        print("Player2 wins and has a score of",score2)
          elif item1 == "Scissors":
                 if item2 == "Paper":
                        score1 +=1
                        print("Player1 wins and has a score of",score1)
                 else:
                        score2 +=1
                        print("Player2 wins and has a score of",score2)
          elif item1 == "Paper":
                 if item2 == "Rock":
                        score1 +=1
                        print("Player1 wins and has a score of",score1)
                 else:
                        score2 +=1
                        print("Player2 wins and has a score of",score2)
          else:
                 print("Invalid input, try again")

game()

输出:

Rock,Paper or Scissors?(1) Rock
Rock,Paper or Scissors?(2) Paper
Player2 wins and has a score of 1
Rock,Paper or Scissors?(1) Rock
Rock,Paper or Scissors?(2) Paper
Player2 wins and has a score of 2
Rock,Paper or Scissors?(1) Rock
Rock,Paper or Scissors?(2) Paper
Player2 wins and has a score of 3

Process finished with exit code 0

让我为你分解,

while score1 <= 3 or score2 <= 3 

说的是score1小于3或者score2小于3,这里要考虑两个变量。试试这个。

while score1 < 3 and score2 < 3