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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With