Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function in another function causing error due to arguments in parantheses

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!

like image 912
Joey Avatar asked Dec 20 '25 23:12

Joey


1 Answers

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

like image 100
Bhargav Rao Avatar answered Dec 23 '25 11:12

Bhargav Rao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!