Python 2 - Rock, Paper, Scissors

Hello,
When I run the below code I get an error that user_choice is undefined. However, it is defined a few lines above. Am I misunderstanding something about how this should be defined? Am I overlooking a typo? Is this a bug in the codecademy workspace? I’d really appreciate any feedback here as I am very new.

“”“Prompt user to select Rock, Paper, or Scissors. Instruct computer to randomly make a selection of those choices. Compare the user and computer selections. Determine winner. Inform user of result.”“”

from random import randint

options = [“ROCK”, “PAPER”, “SCISSORS”]
message = {
‘tie’: “Yawn it’s a tie!”,
‘won’: “Yay you won!”,
‘lost’: “Aww you lost!”
}

def decide_winner(user_choice, computer_choice):
print str(user_choice)
print str(computer_choice)
if user_choice == computer_choice:
print message[‘tie’]
elif user_choice == options[0] and computer_choice == options[2]:
print message[‘won’]
elif user_choice == options[1] and computer_choice == options[0]:
print message[‘won’]
elif user_choice == options[2] and computer_choice == options[1]:
print message[‘won’]
else:
print message[‘lost’]

def play_RPS():
user_choice = raw_input("Enter ROCK, PAPER, or SCISSORS: ")
user_choice = user_choice.upper()
computer_choice = options[randint(0,2)]

decide_winner(user_choice, computer_choice)
play_RPS()

for some reason the indents didn’t post but they are there.

Hi, this is how I believe your code should be indented. The way decide_winner is separate to the rest of the function makes me wonder if that’s where your issue could be.

from random import randint

options = [“ROCK”, “PAPER”, “SCISSORS”]
message = {
‘tie’: “Yawn it’s a tie!”,
‘won’: “Yay you won!”,
‘lost’: “Aww you lost!”
}

def decide_winner(user_choice, computer_choice):
  print str(user_choice)
  print str(computer_choice)
  if user_choice == computer_choice:
    print message[‘tie’]
  elif user_choice == options[0] and computer_choice == options[2]:
    print message[‘won’]
  elif user_choice == options[1] and computer_choice == options[0]:
    print message[‘won’]
  elif user_choice == options[2] and computer_choice == options[1]:
    print message[‘won’]
  else:
    print message[‘lost’]

def play_RPS():
  user_choice = raw_input("Enter ROCK, PAPER, or SCISSORS: ")
  user_choice = user_choice.upper()
  computer_choice = options[randint(0,2)]
  decide_winner(user_choice, computer_choice)

play_RPS()

Incidentally, to post code you can select the < / > option at the top of the text box and then paste it into the block that appears. It’ll show up with indents and such like in place.

Hope that helps.

Didn’t see this until long after I figured it out lol. Thank you @pluginmaybe for the reply and the tip about the </> option. I had not noticed that.
Just in case anyone else runs into the same issue, yes, the decide_winner function had to be called within the play_RPS function. Cheers to fumbling through new concepts.

1 Like