Python:TypeError:' list'对象在全局变量

时间:2016-07-21 16:59:43

标签: python python-2.7

我目前正在使用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()

思想?

1 个答案:

答案 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