1

This is the code that I am working on right now:

def getWinner(userChoice, computerChoice):

    if userChoice == "rock" and computerChoice == "scissors":
       winner = userChoice
    elif userChoice == "paper" and computerChoice == "rock":
       winner = userChoice
    elif userChoice == "scissors" and computerChoice == "paper":
       winner = userChoice
    elif userChoice == "rock" and computerChoice == "paper":
       winner = computerChoice
    elif userChoice == "paper" and computerChoice == "scissors":
      winner = computerChoice
    elif userChoice == "scissors" and computerChoice == "rock":
      winner = computerchoice
    elif userChoice == computerChoice:
      winner = "It's a tie."
return(winner)

userChoice = input("Enter your choice:")
computerChoice = print(getComputerChoice())
winnerOfGame = getWinner(userChoice, computerChoice)
print(winnerOfGame)

I am trying to set up a rock, paper, scissors game, but every time I try to run this function, it returns with:

Traceback (most recent call last):
   File "C:/Python34/idk 2.py", line 45, in <module>
    winnerOfGame = getWinner(userChoice, computerChoice)
   File "C:/Python34/idk 2.py", line 41, in getWinner
    return(winner)
UnboundLocalError: local variable 'winner' referenced before assignment

I have tried assigning a global variable, but nothing seems to be working when I try to fix it. When I do other if statements like this, I do not have issues with the variable being referenced before assignment, and I am doing nothing differently.

cpurcell
  • 21
  • 1
  • 2

2 Answers2

1

You just need to declare winner at the beginning of the function. You don't need global variable here. You also need to specify what should be the result if the userChoice or computerChoice have none of the expected values. You should return an error probably.

def getWinner(userChoice, computerChoice):
    winner = "" #***************
    if userChoice == "rock" and computerChoice == "scissors":
        winner = userChoice
    elif userChoice == "paper" and computerChoice == "rock":
        winner = userChoice
    elif userChoice == "scissors" and computerChoice == "paper":
        winner = userChoice
    elif userChoice == "rock" and computerChoice == "paper":
        winner = computerChoice
    elif userChoice == "paper" and computerChoice == "scissors":
       winner = computerChoice
    elif userChoice == "scissors" and computerChoice == "rock":
       winner = computerchoice
    elif userChoice == computerChoice:
       winner = "It's a tie."
    else:
       winner = "Wrong input! Try again" #************
    return winner

userChoice = input("Enter your choice:")
computerChoice = print(getComputerChoice())
winnerOfGame = getWinner(userChoice, computerChoice)
print(winnerOfGame)
afsafzal
  • 592
  • 5
  • 15
  • 2
    if the `winner` in the `else` statement is defined, why would one need to declare winner before the `if-else` block? – virtualxtc Sep 11 '18 at 00:52
  • 1
    That's correct. If you have an `else` statement that defines `winner`, there's no need in defining it beforehand. But if you don't have `else` (as the original question), then the declaration is needed. – afsafzal Sep 11 '18 at 18:15
0

The error you get occurs when either of the two arguments has a value that is different from the three expected values (could be a simple spelling mistake). In that case winner never gets defined, and so when you get to return winner, Python does not know what you are talking about :-)

So you should check for such a condition.

As we are at it, you could simplify your code a bit. I suggest using an array with the three possible values. The player that has the value that precedes the other immediately (round robin wise) wins:

def getWinner(userChoice, computerChoice):
    options = ["rock", "scissors", "paper"]
    # check that the two arguments have valid values:
    try:
        userChoiceId = options.index(userChoice);
    except ValueError:
        return "invalid choice: {}".format(userChoice)
    try:
        computerChoiceId = options.index(computerChoice)
    except ValueError:
        return "invalid choice: {}".format(computerChoice)
    # now there are only three outcomes:
    if userChoiceId == computerChoiceId:
        winner = "It's a tie."
    elif (userChoiceId + 1) % 3 == computerChoiceId: 
        winner = userChoice
    else:
        winner = computerChoice
    return winner
trincot
  • 317,000
  • 35
  • 244
  • 286