Please HELP with


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

Trackback (most recent call last):
File "", 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..."
                  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!"
                      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: ")
    user_choice = user_choice.upper()
    computer_choice = options[randint(0, len(options)-1)]

decide_winner(user_choice, computer_choice)



decide_winner(user_choice, computer_choice)

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


I try to define through

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

It's isn't enough?


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


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