Search code examples
pythonloopsvariablesinputreset

Why am I not getting the correct outputs for my game?


I am very new to Python and coding in general. I tried to create my own Rock, Paper, Scissors game. The issue I'm getting can be tested by doing the following: typing "rock", then typing "yes" to play again and repeating those two steps. Eventually, it gives me an "Invalid Input." It is only supposed to print this if the user types something besides rock, paper, or scissors. Is there any way I can fix this? I'd really appreciate any input and advice. Thank you for your time.

import random

# to loop the program
def Repeat():
  Answer = input ("Do you want to play again? (Y/N): ")
  if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
    Game()
  if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
    exit()
  else:
    print ("Invalid Input")
    Repeat()

def Game():

# Variable Reset
  Choice = None
  Opponent_Choice = None
  
# Choices
  Opponent_Choices = ["Rock", "Paper", "Scissors"]
  Choice = input ("Type Rock, Paper, or Scissors: ")
  Opponent_Choice = random.choice(Opponent_Choices)
  
# Outcomes
  # if you choose rock
  if Choice == "Rock" or Choice == "rock": 
    if Opponent_Choice == "Paper":
      print ("your opponent chose Paper. You lose!")
      Repeat()
    elif Opponent_Choice == "Scissors":
      print ("your opponent chose Scissors. You win!")
      Repeat()
    elif Opponent_Choice == "Paper":
      print ("your opponnent chose Rock. It was a tie!")
      Repeat()
  # if you choose paper    
  elif Choice == "Paper" or Choice == "paper": 
    if Opponent_Choice == "Paper":
      print ("your opponent chose Paper. It was a tie!")
      Repeat()
    elif Opponent_Choice == "Scissors":
      print ("your opponent chose Scissors. You lose!")
      Repeat()
    elif Opponent_Choice == "Paper":
      print ("your opponent chose Rock. You win!")
      Repeat()
  # if you choose scissors    
  elif Choice == "Scissors" or Choice == "scissors": 
    if Opponent_Choice == "Paper":
      print ("your opponent chose Paper. You win!")
      Repeat()
    elif Opponent_Choice == "Scissors":
      print ("your opponent chose Scissors. It was a tie!")
      Repeat()
    elif Opponent_Choice == "Paper":
      print ("your opponet chose Rock. You lose!")
      Repeat()
  else:
    print ("Invalid input")
    Game()
Game()

p.s. Sorry for the messy code :P Still very new to this.

The actual issue: The actual issue


Solution

  • The reason you seem to be getting the invalid input is due to your check for the Opponent_choice not checking for Rock but instead checking for paper twice. Therefore it jumps all the way to the else statement at the end of the function which contains the invalid input print statement if the opponent chooses Rock.

    if Opponent_Choice == "Paper":
          print ("your opponent chose Paper. You lose!")
          Repeat()
        elif Opponent_Choice == "Scissors":
          print ("your opponent chose Sciccors. You win!")
          Repeat()
        elif Opponent_Choice == "Paper": #error here
          print ("your opponnet chose Rock. It was a tie!")
          Repeat()
    

    The fix would be

    if Opponent_Choice == "Paper":
          print ("your opponent chose Paper. You lose!")
          Repeat()
        elif Opponent_Choice == "Scissors":
          print ("your opponent chose Sciccors. You win!")
          Repeat()
        elif Opponent_Choice == "Rock": #change made here
          print ("your opponnet chose Rock. It was a tie!")
          Repeat()