python – 在另一个函数中调用一个函数,导致由于parantheses中的参数而导致错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在另一个函数中调用一个函数,导致由于parantheses中的参数而导致错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2234字,纯文字阅读大概需要4分钟。
内容图文
碰巧我正在使用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']
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和用户函数,但您没有将它们分配给任何变量.因此,您需要重新定义您的功能游戏
def game():
move = CPU(possibilities)
choice = User()
compare(move, choice)
通过这种方式,您将调用函数compare与调用其他两个函数后返回的值的本地副本.
您可以参考官方的documentation,更多地了解函数和return语句
内容总结
以上是互联网集市为您收集整理的python – 在另一个函数中调用一个函数,导致由于parantheses中的参数而导致错误全部内容,希望文章能够帮你解决python – 在另一个函数中调用一个函数,导致由于parantheses中的参数而导致错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。