Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

Python: TypeError: 'list' object is not callable on global variable

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.

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()

Edit:

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()

Thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are mixing the name of a list with that of a function:

help = ["Exit [direction], Open [object], Talk to [Person], Use [Item]"]

And then:

def help():
    sub_help = '|'.join(help_commands)
    print "The following commands are avalible: " + sub_help

The name help in the current scope (which references a list) is being treated as a callable, which is not the case.

Consider renaming the list or better still, both, since the name help is already being used by a builtin function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...