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

2024/9/20 2:31:12

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_helpdef 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_nameprint "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_helpdef 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_nameprint "It is nice to meet you, ", player['name'] + "."help_test()

Thoughts?

Answer

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.

https://en.xdnf.cn/q/119350.html

Related Q&A

Python beautifulsoup how to get the line after href

I have this piece of html:<a href="http://francetv.fr/videos/alcaline_l_instant_,12163184.html" class="ss-titre">"Paris Combo" </a> <…

Scrapy empty output

I am trying to use Scrapy to extract data from page. But I get an empty output. What is the problem? spider: class Ratemds(scrapy.Spider):name = ratemdsallowed_domains = [ratemds.com]custom_settings =…

nested classes - how to use function from parent class?

If I have this situation:class Foo(object):def __init__(self):self.bar = Bar()def do_something(self):print doing somethingclass Bar(object):def __init(self):self.a = adef some_function(self):I want to …

CUDA Function Wont Execute For Loop on Python with Numba

Im trying to run a simple update loop of a simulation on the GPU. Basically there are a bunch of "creatures" represented by circles that in each update loop will move and then there will be a…

Implementing the Ceaser Cipher function through input in Python

Im trying to create a Ceaser Cipher function in Python that shifts letters based off the input you put in.plainText = input("Secret message: ") shift = int(input("Shift: "))def caes…

Twitter scraping of older tweets

I am doing a project in which I needed to get tweets from twitter, and I used the twitter API but it only gives tweets from 7-9 days old but I want a few months older tweets as well. So I decided to sc…

Bootstrap Navbar Logo not found

Hello I am trying to get my NavBar on bootstrap to show a Logo, I have tried moving the png to different folders in the app but I get this error: System check identified no issues (0 silenced). January…

Why camelcase not installed?

i try to install camelcase in my python project. pip install camelcase but when i want to use the package, pylance give me this error: Import "camelcase" could not be resolved Pylance (report…

Find the two longest strings from a list || or the second longest list in PYTHON

Id like to know how i can find the two longest strings from a list(array) of strings or how to find the second longest string from a list. thanks

Which tensorflow-gpu version is compatible with Python 3.7.3

Actually, I am tired of getting "ImportError: DLL load failed" inWindows 10 CUDA Toolkit 10.0 (Sept 2018) Download cuDNN v7.6.0 (May 20, 2019) / v7.6.4 tensorflow-gpu==1.13.1 / 1.13.2 / 1.14 …