Это работает именно так, как я хочу, за исключением того, что я не могу остановить его, когда пользователь вводит недопустимую опцию.Это игра «Рок, бумага, ножницы», которая не только регистрирует вводимые пользователем данные, но и сохраняет счет текущего раунда и сохраняет итоговый счет всех раундов, пока игра не закончится ... что, как сейчас, никогда не происходит.Как мне закончить эту игру, когда пользователь вводит неверную опцию?Я пытался использовать break, но это неверно.
def rock_paper_scissors():
playerScore = 0
computerScore = 0
print("")
player = input("Choose Rock, Paper, or Scissors: ")
player = player.lower()
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
if player == computer:
print("I chose " + str(computer) + " and you chose " + player + ". It's a tie!")
elif player == "rock" and computer == "scissors":
playerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
elif player == "paper" and computer == "rock":
playerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
elif player == "scissors" and computer == "paper":
playerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". Congratulations! You won! " + player + " beats " + str(computer) + ".")
elif computer == "rock" and player == "scissors":
computerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
elif computer == "paper" and player == "rock":
computerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
elif computer == "scissors" and player == "paper":
computerScore += 1
print("I chose " + str(computer) + " and you chose " + player + ". You lost! " + str(computer) + " beats " + player + ".")
else:
print("Sorry, but you entered an invalid option. The game has ended. See below for the final score. Thank you for playing")
print("")
print("Your score:", str(playerScore) + ", Computer score:", str(computerScore))
return playerScore, computerScore
playerFinal = 0
computerFinal = 0
while True:
player, computer = rock_paper_scissors()
playerFinal += player
computerFinal += computer
print("Your score:", str(playerFinal) + ", Computer score:", computerFinal)