Python:类型错误:“列表"对象不可在全局变量上调用 [英] Python: TypeError: 'list' object is not callable on global variable
问题描述
我目前正在使用 Python 编写基于文本的冒险作为学习练习.我希望帮助"成为一个全局命令,作为值存储在列表中,可以(基本上)随时调用.当玩家进入新房间或帮助选项发生变化时,我会使用新值重置 help_commands
列表.但是,当我调试以下脚本时,我得到一个 'list' object is not callable
TypeError.
I am currently in the process of programming a text-based adventure in Python as a learning exercise. I want "help" to be a global command, stored as values in a list, that can be called at (essentially) any time. As the player enters a new room, or the help options change, I reset the help_commands
list with the new values. However, when I debug the following script, I get a 'list' object is not callable
TypeError.
我一遍又一遍地检查我的代码,似乎无法弄清楚出了什么问题.我对 Python 有点陌生,所以我认为这是我忽略的一些简单的事情.
I have gone over my code time and time again and can't seem to figure out what's wrong. I'm somewhat new to Python, so I assume it's something simple I'm overlooking.
player = {
"name": "",
"gender": "",
"race": "",
"class": "",
"HP": 10,
}
global help_commands
help_commands = ["Save", "Quit", "Other"]
def help():
sub_help = '|'.join(help_commands)
print "The following commands are avalible: " + sub_help
def help_test():
help = ["Exit [direction], Open [object], Talk to [Person], Use [Item]"]
print "Before we go any further, I'd like to know a little more about you."
print "What is your name, young adventurer?"
player_name = raw_input(">> ").lower()
if player_name == "help":
help()
else:
player['name'] = player_name
print "It is nice to meet you, ", player['name'] + "."
help_test()
你就像我的 Python 大师,Moses.这解决了我的问题,但是现在我无法让 help_commands 中的值被新命令覆盖:
You're like my Python guru, Moses. That fixed my problem, however now I can't get the values in help_commands to be overwritten by the new commands:
player = {
"name": "",
"gender": "",
"race": "",
"class": "",
"HP": 10,
}
# global help_commands
help_commands = ["Save", "Quit", "Other"]
def help():
sub_help = ' | '.join(help_commands)
return "The following commands are avalible: " + sub_help
def help_test():
print help()
help_commands = ["Exit [direction], Open [object], Talk to [Person], Use [Item]"]
print help()
print "Before we go any further, I'd like to know a little more about you."
print "What is your name, young adventurer?"
player_name = raw_input(">> ").lower()
if player_name == "help":
help()
else:
player['name'] = player_name
print "It is nice to meet you, ", player['name'] + "."
help_test()
想法?
推荐答案
您将列表的名称与函数的名称混在一起:
You are mixing the name of a list with that of a function:
help = ["Exit [direction], Open [object], Talk to [Person], Use [Item]"]
然后:
def help():
sub_help = '|'.join(help_commands)
print "The following commands are avalible: " + sub_help
当前作用域(引用列表)中的名称 help
被视为可调用的,事实并非如此.
The name help
in the current scope (which references a list) is being treated as a callable, which is not the case.
考虑重命名列表或更好,两者兼而有之,因为名称 help
已被内置函数使用.
Consider renaming the list or better still, both, since the name help
is already being used by a builtin function.
这篇关于Python:类型错误:“列表"对象不可在全局变量上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!