Python 在另一个函数中调用一个函数,由于parantesse中的参数而导致错误
碰巧我刚刚开始使用Python编程,我正要编写一个小的石头剪刀游戏 不幸的是,当我尝试运行脚本时,我收到以下错误:Python 在另一个函数中调用一个函数,由于parantesse中的参数而导致错误,python,function,Python,Function,碰巧我刚刚开始使用Python编程,我正要编写一个小的石头剪刀游戏 不幸的是,当我尝试运行脚本时,我收到以下错误: file rps.py, line 53 in game compare (move,choice) NameError: name 'move' is not defined" 以下是我目前的代码: from random import randint possibilities = ['rock', 'paper', 'scissors'] de
file rps.py, line 53 in game
compare (move,choice)
NameError: name 'move' is not defined"
以下是我目前的代码:
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()
当我定义函数compare(c,u)
并在括号中添加参数“c”和“u”时,我肯定我做错了什么。
我想我之前通过使用return语句来确保能够使用这些变量
一般来说,我对编程很陌生,因此没有经验,所以请友好一点 问题在于,您只调用函数
CPU
和User
,而没有将它们分配给任何变量。因此,您需要重新定义函数游戏
,如中所示
def game():
move = CPU(possibilities)
choice = User()
compare(move, choice)
这样,在调用其他两个函数后,您将调用函数compare
,并使用值return
ed的本地副本
通过引用官方的与括号无关,您可以参考有关函数和返回语句的更多信息。阅读错误:它表示范围中没有名称
move
(变量、函数)。仅此而已。此外,请确保张贴有效(格式化)代码。