0

This works exactly as I want it to, with the exception that I can't get it to stop when the user enters an invalid option. It's a Rock, Paper, Scissors game that not only registers the user's input but it keeps score of the current round and keeps the final score of all rounds until the game ends... which as it is right now, never happens. How do I end this game when the user inputs an invalid option? I tried using break, but it's invalid.

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)

4 Answers 4

1

This can be solved by simply adding a flag to check whether the while True loop needs to be ended. Here:

import random
def rock_paper_scissors():
    playerScore = 0
    computerScore = 0
    flag = False

    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:
        flag = True
        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, flag

playerFinal = 0
computerFinal = 0

while True:
    player, computer, flag = rock_paper_scissors()
    playerFinal += player
    computerFinal += computer
    print("Your score:", str(playerFinal) + ", Computer score:", computerFinal)
    if flag:
        break
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! Thank you very much for explaining that and showing it.
1

If the returned scores are both equal to zero, the player entered incorrect input and you can break the loop.

while True:
    player, computer = rock_paper_scissors()
    if player == 0 and computer == 0:
        break
    playerFinal += player
    computerFinal += computer
    print("Your score:", str(playerFinal) + ", Computer score:", computerFinal)

1 Comment

Thank you for your response. I tried that, but it just reset the final scores to 0. I need them to print the final score of all the rounds the user played when the game ends.
0

Just add break after the invalid choice You can let the player scroll at that time with minus

if player < 0
   playerFinal = -1 * player
   break

1 Comment

This doesn't work because the choice is made inside the rock_paper_scissors() function, but the loop is at the main level.
0

Change your looping condition from:

while True:

to:

while True and (player+computer) != 0 :

With an invalid user choice, the score for that round will be 0 and next time the loop will not pass the condition.

2 Comments

This is an error, because player and computer aren't defined until the loop is executed at least once.
Correct! You have to initialize them alongwith playerFinal and computerFinal. Assigning value of -1 should be fine so they dont add up to 0 and the loop is entered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.