Reputation: 109
So I'm working on a text adventure game in Python 3, and what I do not understand is how def available_actions(self):
is supposed to work.
The file below is my gamedata function where I handle the Location, Items, and World:
class Location:
def __init__(self, brief_description, full_description, available_actions):
'''Creates a new location.
ADD NEW ATTRIBUTES TO THIS CLASS HERE TO STORE DATA FOR EACH LOCATION.
Data that could be associated with each Location object:
a position in the world map,
a brief description,
a long description,
a list of available commands/directions to move,
items that are available in the location,
and whether or not the location has been visited before.
Store these as you see fit.
This is just a suggested starter class for Location.
You may change/add parameters and the data available for each Location class as you see fit.
The only thing you must NOT change is the name of this class: Location.
All locations in your game MUST be represented as an instance of this class.
'''
self.get_brief_description = brief_description
self.get_full_description = full_description
self.available_actions = available_actions
def get_brief_description (self):
'''Return str brief description of location.'''
return self.brief_description
def get_full_description (self):
'''Return str long description of location.'''
return self.full_description
def available_actions(self):
'''
-- Suggested Method (You may remove/modify/rename this as you like) --
Return list of the available actions in this location.
The list of actions should depend on the items available in the location
and the x,y position of this location on the world map.'''
return self.available_actions
This is the second file below called adventure.py
where I'm supposed to work on the program of the game itself.
import gamedata
if __name__ == "__main__":
WORLD = World("map.txt", "locations.txt", "items.txt")
PLAYER = Player(0,0) # set starting location of player; you may change the x, y coordinates here as appropriate
menu = ["look", "inventory", "score", "quit", "back"]
while not PLAYER.victory:
location = WORLD.get_location(PLAYER.x, PLAYER.y)
# ENTER CODE HERE TO PRINT LOCATION DESCRIPTION
# depending on whether or not it's been visited before,
# print either full description (first time visit) or brief description (every subsequent visit)
print("What to do? \n")
print("[menu]")
for action in location.available_actions():
print(action)
choice = input("\nEnter action: ")
if (choice == "[menu]"):
print("Menu Options: \n")
for option in menu:
print(option)
choice = input("\nChoose action: ")
When I run the file adventure.py
, I get an error that says
AttributeError: 'NoneType' object has no attribute 'available_actions'```
This error would be on this line:
for action in location.available_actions():
Is the reason why I get this error because of the
def available_actions(self):
'''
-- Suggested Method (You may remove/modify/rename this as you like) --
Return list of the available actions in this location.
The list of actions should depend on the items available in the location
and the x,y position of this location on the world map.'''
return self.available_actions
In the gamedata function? I'm unsure of how to go about this part of the code and how to fix this error.
Upvotes: 1
Views: 210
Reputation: 87
Your available_action method definition is different from how you use it in adventure.py
You don't pass self into every method, it is implied through instance variables. Passing self will add a variable to the method which, if not addressed in the code, will cause the above error since the signatures do not match. Just remove self from each method in Location.py
Upvotes: 0
Reputation: 46
The problem should be in the line
location = WORLD.get_location(PLAYER.x, PLAYER.y)
since in this line the variable "location" is assigned and the error tells you it is a 'NoneType' Object. Search for the problem in get_location
of your class WORLD
and make sure you return the right thing.
Upvotes: 3
Reputation: 3674
It looks like your World class should be creating Location objects that receive a menu of available options. Your Location constructor should probably specify that "available_actions" is not empty, since that causes an error later in the code. And I would imagine in this kind of game, if you were ever in a location with no available actions, the game would just stop.
Upvotes: 0