Please HELP with RPS.py


#1



Hi guys,
I had problem with create RPS.py... please give advice!

Trackback (most recent call last):
File "RPS.py", line 37, in
decide_winner(user_choice, computer_choice)
NameError: name 'user_choice' is not defined

How can i fix it? I try to figure out, but can't.


"""
This is for
Rock - Paper - Scissors game
"""

from random import randint
from time import sleep
options = ["R", "P", "S"]
LOSS_MESSAGE = "You lost!"
WIN_MESSAGE = "You Win!"

def decide_winner(user_choice, computer_choice):
                  print "You selected: %s" % user_choice
                  print "Computer selecting..."
                  sleep(1)
                  print "computer selected %s" % computer_choice
                  user_choice_index = option.index(user_choice)
                  computer_choice_index = options.index(computer_choice)
                  if user_choice_index == computer_choice_index:
                      print "It's a tie!"
                  elif user_choice_index == 0 and computer_choice_index == 2 or user_choice_index == 1 and computer_choice_index == 0 or user_choice_index == 2 and computer_choice_index == 1:
                      print WIN_MESSAGE
                  elif user_choice_index > 2:
                      print "Invalid input, Try again!"
                      return
                  else:
                      print LOSS_MESSAGE
                  
def play_RPS():
    print "Let's play Rock Paper Scissors!"
    user_choice = raw_input("Select R for Rock, P for Paper, S for Scissors: ")
    sleep(1)
    user_choice = user_choice.upper()
    computer_choice = options[randint(0, len(options)-1)]

decide_winner(user_choice, computer_choice)
play_RPS()


#2

here:

decide_winner(user_choice, computer_choice)

decide_winner should be part of play_RPS, all variable you need are defined in play_RPS


#3

I try to define through

    user_choice = raw_input("Select R for Rock, P for Paper, S for Scissors: ")

It's isn't enough?


#4

so now user_choice is a local variable in the play_RPS function, which is why when you call determine_winner inside the play_RPS function.

Also, currently decide_winner is called before play_RPS which means user_choice is not defined at all


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.