Hello all. I am trying to learn some python by creating a text adventure game. I am using the book - Make Your Own Python Text Adventure by Phillip Johnson as a reference but I am stuck on getting my inventory to display.
So far I have a folder with 3 document? Modules? I am not sure the correct word.
player.py and items.py come back as good but when i try and pull up my inventory on game.py file it comes back with this error. AttributeError: ‘Player’ object has no attribute ‘inventory’ player.py, line 13
This is my game.py file
from player import Player
def play():
print("Welcome to Gateway!")
player = Player()
while True:
action_input = get_player_command()
if action_input in ['n', 'N', '8']:
print("Go North")
elif action_input in ['s', 'S', '2']:
print("Go South")
elif action_input in ['e', 'E', '6']:
print("Go East")
elif action_input in ['w', 'W', '4']:
print("Go West")
elif action_input in ['i', 'I']:
player.print_inventory()
else:
print("Invalid Action")
def get_player_command():
return input('Action: ')
play()
This is my player.py file
import items
class Player:
def _init_(self):
self.inventory = [items.Rock(),
items.Dagger(),
'Gold(5)',
'Crusty Bread']
def print_inventory(self):
print("Inventory:")
for item in self.inventory:
print('* ' + str(item))
best_weapon = self.most_powerful_weapon()
print("Your best weapon is your {}".format(best_weapon))
def most_powerful_weapon(self):
max_damage = 0
best_weapon = none
for item in self.inventory:
try:
if item.damage > max_damage:
best_weapon = item
max_damage = item.damage
except AttributeError:
pass
return best_weapon
This is my items.py file
class Weapon:
def _init_(self):
raise NotImplementedError("Do not create raw Weapon objects.")
def _str_(self):
return self.name
class Rock(Weapon):
def _init_(self):
self.name = "Rock"
self.description = "A fist-sized rock, suitable for bludgeoning."
self.damage = 5
class Dagger(Weapon):
def _init_(self):
self.name = "Dagger"
self.description = "A small dagger with some rust. " \
"Somewhat more dangerous than a rock."
self.damage = 10
class RustySword(Weapon):
def _init_(self):
self.name = "Rusty Sword"
self.description = "This sword is showing its age, " \
"but still has some fight in it."
self.damage = 20
All of the other commands in the game.py file work, the only one that returns with an error is inventory. I have been stuck on this for two days and I do not want to move on until this is resolved.
Thank you