为什么我的代码在while statment结束后运行while语句?
我正在尝试制作一个岩石,纸,剪刀游戏,但是每次我运行它,当游戏结束时,它都会要求用户再次输入岩石,纸或剪刀。它不会将额外的回合结果添加到计数器中,只要输入某些内容,用户输入什么都没关系。我一直陷入困境,对我来说,整个代码看起来都不错,但是我希望在计算机或用户获得3次胜利后立即结束。这是代码:
import random
print("Welcome to rock, paper, scissors, where you will verse a computer, and the first one to win 3 rounds, wins!\n"
"to chose rock, enter r, to chose paper, enter p, to enter scissors enter s.")
computer = random.choice("rps")
print(computer)
player = str(input("Rock, Paper, Scissors, Shoot! "))
player_counter = 0
computer_counter = 0
player_win = 0
computer_win = 0
while player_counter <= 2 and computer_counter <= 2:
if player == "s" and computer == "s" or player == "p" and computer == "p" or player == "r" and computer == "r":
print("You and the computer tied!")
computer = random.choice("rps")
print(computer)
print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
player = str(input("Rock, Paper, Scissors, Shoot! "))
elif player == "r" and computer == "p" or player == "p" and computer == "s" or player == "s" and computer == "r":
print("computer wins!")
computer_counter += 1
computer = random.choice("rps")
print(computer)
print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
player = str(input("Rock, Paper, Scissors, Shoot! "))
elif player == "r" and computer == "s" or player == "p" and computer == "r" or player == "s" and computer == "p":
print("You win!")
player_counter += 1
computer = random.choice("rps")
print(computer)
print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
player = str(input("Rock, Paper, Scissors, Shoot! "))
if player_counter == 3:
print("You win the game!")
elif computer_counter == 3:
print("You lose the game!")
I'm trying to make a rock, paper, scissors game, but each time I run it, when the game is supposed to be over, it asks the user to input rock, paper or scissors again. It doesn't add the result of that extra round into the counter, and it doesn't matter what the user input as long as the input something. I've been stuck at this and to me the entire code looks fine, but I want it to end right after either the computer or user get 3 wins. Here is the code:
import random
print("Welcome to rock, paper, scissors, where you will verse a computer, and the first one to win 3 rounds, wins!\n"
"to chose rock, enter r, to chose paper, enter p, to enter scissors enter s.")
computer = random.choice("rps")
print(computer)
player = str(input("Rock, Paper, Scissors, Shoot! "))
player_counter = 0
computer_counter = 0
player_win = 0
computer_win = 0
while player_counter <= 2 and computer_counter <= 2:
if player == "s" and computer == "s" or player == "p" and computer == "p" or player == "r" and computer == "r":
print("You and the computer tied!")
computer = random.choice("rps")
print(computer)
print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
player = str(input("Rock, Paper, Scissors, Shoot! "))
elif player == "r" and computer == "p" or player == "p" and computer == "s" or player == "s" and computer == "r":
print("computer wins!")
computer_counter += 1
computer = random.choice("rps")
print(computer)
print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
player = str(input("Rock, Paper, Scissors, Shoot! "))
elif player == "r" and computer == "s" or player == "p" and computer == "r" or player == "s" and computer == "p":
print("You win!")
player_counter += 1
computer = random.choice("rps")
print(computer)
print("Your score is:", player_counter, "and the computer's score is:", computer_counter)
player = str(input("Rock, Paper, Scissors, Shoot! "))
if player_counter == 3:
print("You win the game!")
elif computer_counter == 3:
print("You lose the game!")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于,循环中的所有内容都是偏移 - 您在循环重新启动之前和获胜条件进行重新检查之前开始了新的回合。在下一个通过循环中,它终止了。
您可以通过简单地将选择在循环开始时,在一个位置放置,而不是让它复制+粘贴到每个单独的
时,可以修复此错误和删除许多不必要的代码。 /code> block 分数已更新:
The problem is that everything in your loop is offset — you start a new round before the loop restarts and before the winning condition is rechecked. On the next pass through the loop, it terminates.
You can fix this bug and delete a lot of unnecessary code by simply putting the choice at the start of the loop, in one spot, rather than having it copy+pasted into every individual
if
block after the score has been updated:首先,您输入循环3次并阅读输入3次,但是您在此处循环前一次读取输入,
player = str(输入(“岩石,纸,剪刀,拍摄!”)))))
这就是为什么您会看到不必要的输入。
其次,您应仅在循环的开头而不是在每个条件的内部读取输入。这是一个修改的工作代码
First of all, you enter the loop 3 times and read the input 3 times, but you read the input one time before the loop here,
player = str(input("Rock, Paper, Scissors, Shoot! "))
that's why you see that unnecessary input.
Secondly, you should read input only at the beginning of the loop, not inside of each condition. Here is a modified working code
每次您遇到条件时,都无需编写输入并使用Random.Choice()语句。您可以在循环开始时同时移动输入和随机。选择()。这也将解决计算机或用户赢得游戏后输入输入的问题。
There is no need to write the input and use random.choice() statement each time you run into a condition. YOu can move both the input and the random.choice() at the start of the loop. This will also solve the problem of entering input after the computer or the user wins the game.