Python Forum
How to fix list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix list index out of range
#18
Is this correct?
        Node_Start = Node(S, [6, 3, 1, 1])
        Node_3 = Node(3, [7, 4, 1, S])
It think your board is still riddled with errors. You would be better off making the wall programmatically. You make too many mistakes when doing it by hand.

This is how I make my board. I use dictionaries instead of arrays
class Action(Enum):
    """Enumeration for movements (actions). Value is (row, column) steps to make move"""
    UP = (1, 0)
    RIGHT = (0, 1)
    DOWN = (-1, 0)
    LEFT = (0, -1)

    def __str__(self):
        """Return arrow associated with action""""
        if self is self.LEFT:
            return '\u2190'
        elif self is self.UP:
            return '\u2191'
        elif self is self.RIGHT:
            return '\u2192'
        elif self is self.DOWN:
            return '\u2193'
        return " "

class Board:
    def __init__(self, goals, traps, walls, rows=4, columns=4):
        """Initialize the board"""
        # Make all the tiles
        self.rows = rows
        self.columns = columns
        self.tiles = {x:Tile(x, x in goals, x in traps, x in walls) for x in range(1, rows*columns+1)}

        # Tell open tiles about their neighbors
        for label in set(self.tiles) - set(goals + traps + walls):
            tile = self.tiles[label]
            for action in Action:
                r, c = action.value
                neighbor = self.tile(r + (label-1) // columns, c + (label-1) % columns)
                # Make a list of neighboring tiles
                tile.neighbor[action] = neighbor if neighbor and not neighbor.wall
                tile.values[action] = 1 if neighbor and neighbor.wall else 0

    def tile(self, label, column):
        """Return tile at intersection of row and column.  Return None if row or column out of range"""
        if 0 <= label < self.rows and 0 <= column < self.columns:
            return self.tiles[label * self.columns + column + 1]
        return None 
Reply


Messages In This Thread
How to fix list index out of range - by longmen - Apr-13-2022, 02:28 AM
RE: How to fix list index out of range - by longmen - Apr-15-2022, 03:54 AM
RE: How to fix list index out of range - by longmen - Apr-18-2022, 03:12 PM
RE: How to fix list index out of range - by longmen - Apr-20-2022, 01:54 AM
RE: How to fix list index out of range - by longmen - Apr-23-2022, 07:02 PM
RE: How to fix list index out of range - by longmen - Apr-24-2022, 02:25 AM
RE: How to fix list index out of range - by longmen - Apr-24-2022, 02:51 PM
RE: How to fix list index out of range - by longmen - Apr-24-2022, 03:42 PM
RE: How to fix list index out of range - by longmen - Apr-24-2022, 05:22 PM
RE: How to fix list index out of range - by deanhystad - Apr-24-2022, 07:14 PM
RE: How to fix list index out of range - by longmen - Apr-24-2022, 10:45 PM
RE: How to fix list index out of range - by longmen - Apr-25-2022, 12:20 AM
RE: How to fix list index out of range - by longmen - Apr-25-2022, 02:22 AM
RE: How to fix list index out of range - by longmen - Apr-25-2022, 05:44 PM
RE: How to fix list index out of range - by longmen - Apr-27-2022, 05:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  list index out of range OliverG 3 2,470 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 3,061 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  List index out of range when turning CSV into dict ranbarr 15 6,923 May-12-2021, 10:38 AM
Last Post: ranbarr
  List vs index: Frederico_Caldas 5 3,821 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,513 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  list index out of range mcgrim 2 3,025 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 4,020 Jan-17-2019, 07:54 AM
Last Post: buran
  String index out of range felie04 2 5,670 Aug-17-2018, 11:18 PM
Last Post: felie04
  Accessing data in zip - Index out of range pythoneer 24 13,486 Mar-15-2018, 06:19 PM
Last Post: buran
  "List index out of range" for output values pegn305 3 5,457 Nov-26-2017, 02:20 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

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