Home > other >  How do I access a value in an object filled dictionary?
How do I access a value in an object filled dictionary?

Time:11-12

Here is the problematic part of the code:

import numpy as np

class Player():
  def __init__(self, health, maxHealth, baseDmg, dmg, name, weapons, items, isAlive, previousRoom, room):
    self.health = health
    self.maxHealth = maxHealth
    self.baseDmg = baseDmg
    self.dmg = dmg
    self.name = name
    self.weapons = weapons
    self.items = items
    self.isAlive = isAlive
    self.previousRoom = previousRoom
    self.coords = (0, 0)

  def Move(self, direction):
    if direction not in self.room.exits:
      print("Cannot Move In That Direction!")
      return
    newRoomName = self.room.exits[direction]
    self.previousRoom = self.room.name
    print("Moving to", newRoomName)
    world[newRoomName] = newRoomName
    self.room = world[newRoomName]

class Room():
  def __init__(self, name, description, exits, hasWeapon, weapon, hasItem, item, hasEnemy, enemy, isFirstVisit, coords):
    self.name = name
    self.description = description
    self.exits = exits
    self.hasWeapon = hasWeapon
    self.weapon = weapon
    self.hasItem = hasItem
    self.item = item
    self.hasEnemy = hasEnemy
    self.enemy = enemy
    self.isFirstVisit = isFirstVisit
    self.coords = coords

world = {}
rooms = np.empty((11,11), object)

rooms[5][5] = Room(
  "room1",
  "",
  {"E": "room2"},
  False,
  None,
  False,
  None,
  False,
  None,
  True,
  (5, 5)
)
rooms[5][6] = Room(
  "room2",
  "",
  {"W": "room1"},
  False,
  None,
  False,
  None,
  False,
  None,
  True,
  (5, 5)
)

counter = 1

world["room1"] = rooms[3][3]
world["room2"] = rooms[3][4]

player = Player(10, 10, 5, 5, "Jedidiah", [], [], True, world["room1"].coords, world["room1"].coords)

What I am trying to do is make a movement system in python using a 2d array and dictionaries (as per the project requirements), but it keeps giving me an error saying that "str object has no attribute 'coords'":

Traceback (most recent call last):
  File "main.py", line 75, in <module>
    player = Player(10, 10, 5, 5, "Ethan", [], [], True, world["room1"].coords, world["room1"].coords)
AttributeError: 'NoneType' object has no attribute 'coords'

I have no idea why this is happening, but I'm sure it is a simple solution. I am new to programming, so please make the answers as simple as possible. All help is appreciated.

CodePudding user response:

np.empty with object dtype makes an array with None elements:

In [184]: rooms = np.empty((2,2), object)
In [185]: rooms
Out[185]: 
array([[None, None],
       [None, None]], dtype=object)

I see you have a set a couple of elements of rooms [5,5] and [5,6] to Room objects, and assigned a couple of other elements to the world dict (3,3) (3,4). These elements are still None.

Looks like all those 'unset' coordinates are giving you problems. Either be more careful about the indexing, or add some safeguards to correctly handle None elements.

  • Related