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

时间:2020-05-16 18:17:56

标签: python-3.x while-loop exit

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

让我为您分解,

while score1 <= 3 or score2 <= 3 

它说,如果score1小于3或score2小于3。在这里,您必须考虑两个变量。试试这个吧。

while score1 < 3 and score2 < 3