0
Follow
2
View

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

qianke321 注册会员
2023-01-26 03:53

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 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()
qklwdd1 注册会员
2023-01-26 03:53

One problem with the code is that you have functions calling themselves. The function Game() and Repeat() in your code do this. This is called recursion and can be very useful. But it can also cause problems if used incorrectly. For one thing, let's consider the code above DOES work, if you played it for very long, you would get a stack overflow exception. I would suggest some research on recursion and "the stack" to understand this better.

If you take out the recursion like so for example: it should work.

I also noticed you are checking user input on the confirmation question. Very nice. But why not for the Rock Paper Scissors selection?

Example refactor without recursion:

import random

# to loop the program
def Repeat():
    while True:
        result = Confirm()
        if (result is True):
            break


def Confirm():
    Answer = input ("Do you want to play again? (Y/N): ")
    if Answer == ("Yes") or Answer == ("yes") or Answer == ("Y") or Answer == ("y"):
        return True
    if Answer == ("No") or Answer == ("no") or Answer == ("N") or Answer == ("n"):
        exit()
    else:
        return False


def Game():
# 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 Sciccors. You win!")
      Repeat()
    elif Opponent_Choice == "Paper":
      print ("your opponnet 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 Sciccors. You lose!")
      Repeat()
    elif Opponent_Choice == "Paper":
      print ("your opponet 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 Sciccors. It was a tie!")
      Repeat()
    elif Opponent_Choice == "Paper":
      print ("your opponet chose Rock. You lose!")
      Repeat()
  else:
    print ("Invalid input")


while True:
    Game()
zssdelaogong 注册会员
2023-01-26 03:53

I can't give you an answer about why there's an error, but here is a clue: the 'invalid input' message you get was from line 11, in Repeat(). The line will only be run if the if statement above it evaluates to false, and the if statement will only be run if the other if statement above it exits, which means you'll only get the message if the Game()-function somehow exited, which shouldn't happen. You can add a print()-statement after line 7 to confirm that. The REAL reason was because you used recursion, like @dominic has said. And you should try their code.

PS: don't feel bad about messy code. Mine is worse than yours. But you really don't have to capitalize the variables and function. Good luck with Python!

About the Author

Question Info

Publish Time
2023-01-26 03:53
Update Time
2023-01-26 03:53