3

As it happens I am just getting into programming with Python and I was about to program a little rock-paper-scissors game.

Unfortunately when I'm trying to run my script, I am receiving the following error:

file rps.py, line 53 in game    
   compare (move,choice)     
  NameError: name 'move' is not defined"

Here's my code so far:

from random import randint
possibilities = ['rock', 'paper', 'scissors']

def CPU(list):
    i =  randint(0, len(list)-1)
    move = list[i]
    #print (str(move))
    return move

def User():
    choice = str(input('Your choice? (Rock [r], Paper[p], Scissors[s])'))
    choice = choice.lower()

    if choice == 'rock' or choice == 'r':
        choice = 'rock'
    elif choice == 'scissors' or choice =='s':
        choice = 'scissors'
    elif choice == 'paper' or choice == 'p':
        choice = 'paper'

    #print ('Your choice: ' + str(choice))
    return choice


def compare(c, u):
    if c == u:
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('That is what we call a tie. Nobody wins.')
    elif c == 'paper' and u == 'rock':
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('This means that you, my friend, lose.')
    elif c == 'paper' and u == 'scissors':
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('Congratulations, you win....this time.')
    elif cc == 'rock' and u == 'paper':
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('Congratulations, you win....this time.')
    elif c == 'rock' and u == 'scissors':
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('This means that you lose.')
    elif c == 'scissors' and u == 'paper':
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('This means that you lose.')
    elif c == 'scissors' and u == 'rock':
         print ('Your choice was: ' + str(u) + 'and I chose: ' + str(c))
         print ('Congratulations, you win....this time.')

def game():
    CPU(possibilities)
    User()
    compare(move, choice)

game()

I am pretty sure that I did something wrong when I defined the function compare(c,u) and added the arguments 'c' and 'u' in the parentheses. I thought that I made sure that I was able to use these variables by using the return statement before.

I am quite new to programming in general and therefore inexperienced, so please be kind!

1
  • Nothing to do with parenthesis. Read the error: it says there is no name move (variable, function) in scope. That is all. Also, please make sure to post valid (formatted) code. Commented Apr 19, 2015 at 20:20

1 Answer 1

4

The problem is that you are only calling the functions CPU and User but you are not assigning them to any variables. Hence you need to re-define your function game as in

def game():
    move = CPU(possibilities)
    choice = User()
    compare(move, choice)

In this way you will be calling the function compare with a local copy of the values returned after calling the two other functions.

You can refer more about functions and the return statement by referring the official documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.