我目前正在使用Python编写基于文本的冒险作为学习练习。我想要"帮助"作为一个全局命令,存储为列表中的值,可以在(基本上)任何时间调用。当玩家进入新房间或帮助选项发生变化时,我会使用新值重置help_commands
列表。但是,当我调试以下脚本时,我得到'list' object is not callable
TypeError。
我一次又一次地检查了我的代码,似乎无法弄清楚错误是什么。我对Python有些陌生,所以我认为它是一个简单易用的东西。
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大师,摩西。这解决了我的问题,但是现在我无法通过新命令覆盖help_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()
思想?
答案 0 :(得分:5)
您正在将列表名称与函数名称混合使用:
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
被视为可调用,但情况并非如此。
考虑重命名列表,或者更好,两者都是,因为内置函数已经在使用名称help
。