Python Forum
I am getting this TypeError: 'TreasureMap' object is not subscriptable.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting this TypeError: 'TreasureMap' object is not subscriptable.
#3
This is the reason for the index error.
if self.map[row][column] == 'x':
self.map is a TreasureMap, but this treats it like a list of lists. You could change to self.map.map[row][column], but I think a better solution is to get rid of the TreasureMap class.

The TreasureMap class does nothing useful. It's two methods are a better fit in the TreasureHunter class.

You could make a useful TreasureMap, something that supports better indexing and printing.
class TreasureMap:
    """A xy map for treasure hunting."""
    def __init__(self, map):
        self.map = map.copy()

    def __getitem__(self, pos):
        """Get value using indexing; self[x, y]"""
        x, y = pos
        return self.map[y][x]

    def __setitem__(self, pos, value):
        """Set valu using indexing; self[x, y] = value"""
        x, y = pos
        self.map[y][x] = value

    def size(self):
        """Return dimensions of map x, y or columns, rows."""
        return len(self.map[0]), len(self.map), 

    def take(self, x, y):
        """Remove item at x, y.  Is it treasure?"""
        item = self[x, y]
        self[x, y] = "o"
        return item

    def empty_copy(self):
        """Return empy may the same size as me."""
        rows, columns = self.size()
        return TreasureMap([['-'] * columns for _ in range(rows)])

    def __str__(self):
        """Return string appropriate for printing,"""
        return "\n".join(" ".join(row) for row in self.map)
To use:
class TreasureHunter():
    """Hunts for treasure on a map.  Keeps notes on where looked."""
    def __init__(self, name, map):
        self.name = name
        self.map = map
        self.notes = map.empty_copy()
        self.treasures = 0

    def move(self, x, y):
        """Move to x, y and dig for treasure."""
        self.notes[x, y] = self.map[x, y]
        if self.notes[x, y] == 'x':
            self.treasures += 1
        return self
Reply


Messages In This Thread
RE: I am getting this TypeError: 'TreasureMap' object is not subscriptable. - by deanhystad - May-25-2024, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 701 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 715 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 973 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 1,388 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,714 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,222 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 5,140 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  Help with python 'not subscriptable' error Extra 3 2,447 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,766 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 2,166 Oct-30-2022, 12:53 PM
Last Post: makeeley

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020