Calling a function in another function causing an error because of the parenthesized arguments

Anyway, I am just programming with Python and I was going to program a little stone paper game.

Unfortunately, when I try to run my script, I get the following error:

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

      

Here's my code:

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 I did something wrong when I defined the function compare(c,u)

and added the 'c' and 'u' arguments in parentheses. I thought I made sure I was able to use these variables using the previous statement.

I'm new to programming in general and therefore inexperienced, so please be so kind!

+3


source to share


1 answer


The problem is you only call functions CPU

and User

, but you don't assign them to any variables. Hence, you need to override your function game

as in

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

      



So you call a function compare

with a local copy of return

ed values after calling two other functions.

return

You can find out more about the functions and instructions by referring to the official documentation

+5


source







All Articles