我是一个初学者,正在为"Rock Paper Scissors";游戏我不想一遍又一遍地运行这个游戏(代码(,因此,使用while循环。现在,在";否则:";当玩家键入任何无效的选择时,计算机也会播放该步骤;无效的选择。再次轮到你了"显示。
我想当玩家键入任何无效的选择时,计算机不应该播放它的回合,我们得到";无效的选择。再次轮到你了"显示,保持游戏运行。
请检查我的代码并指出问题所在。请更正说明。提前感谢!
print("Welcome to the famous Rock Paper Scissors Game. n")
Choices = ["Rock", "Paper", "Scissors"]
while(True):
Player = input("Your turn: ")
Computer = random.choice(Choices)
print(f"Computer's turn: {Computer} n")
if Player == Computer:
print("That's a tie, try again! n")
elif Player == "Rock" and Computer == "Scissors":
print("You Won!!! n")
elif Player == "Rock" and Computer == "Paper":
print("Computer won! n")
elif Player == "Paper" and Computer == "Scissors":
print("Computer won! n")
elif Player == "Paper" and Computer == "Rock":
print("You Won!!! n")
elif Player == "Scissors" and Computer == "Paper":
print("You Won!!! n")
elif Player == "Scissors" and Computer == "Rock":
print("Computer won! n")
else:
print("Invalid choice. Play your turn again! n")
您可以在计算机播放之前检查输入是否有效,并使用continue
-再次询问输入是否无效
Choices = ["Rock", "Paper", "Scissors"]
while(True):
Player = input("Your turn: ")
if Player not in Choices: # If it is not in Choices list above
print("Invalid choice. Play your turn again! n")
continue # This will re run the while loop again.
# If Player gives valid input, then continues this
Computer = random.choice(Choices)
print(f"Computer's turn: {Computer} n")
# The next lines ....
同时查看-中断、继续并通过