Python 3-当满足要求时,while循环不会脱离循环

问题描述 投票:0回答:2

我简单的石头,纸,剪刀代码:

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分时,它才中断循环。为什么?我使用“或”运算符是否错误?

python-3.x while-loop exit
2个回答
0
投票

虽然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

0
投票

让我为您分解,

© www.soinside.com 2019 - 2024. All rights reserved.