Home > Software engineering >  Index out of bounds error that I cannot resolve
Index out of bounds error that I cannot resolve

Time:11-04

I have a very simple python problem, but I fiddled around with the numbers and it still doesn't work.

`import random
 import numpy as np

 class Room:
  def __init__(self, name, contents):
   self.name = name
   self.contents = contents

 rooms = np.zeros((10, 10))

 emptyRooms = []

 halfHeight = int(len(rooms[1]) / 2)
 halfWidth = int(len(rooms[0]) / 2)

 rooms[halfWidth][halfHeight] = 1

 for r in range(len(rooms)):
  for c in range(len(rooms)):
   if rooms[r][c] == 1:
    if rooms[r][c-1] != 1:
     rooms[r][c-1] = 1
    if rooms[r][c 1] != 1:
     rooms[r][c 1] = 1
    if rooms[r-1][c] != 1:
     rooms[r-1][c] = 1
    if rooms[r 1][c] != 1:
     rooms[r 1][c] = 1

 print(rooms)`

And here is the output:

`Traceback (most recent call last):
  File "main.py", line 27, in <module>
   if rooms[r 1][c] != 1:
  IndexError: index 10 is out of bounds for axis 0 with size 10`

Like I said, I have tried fiddles with the numbers and it still comes out to an error. I have no idea how to fix it.

CodePudding user response:

If you want to access one next element in an array using index while iterating through the array, you would need to iterate up to the second last element. Here, you are using rooms[r][c 1] and rooms[r 1][c] considering r and c as indices. While in the last iteration, your index goes out of bounds. So, here you'd need to iterate the loop to the second last index. Try the following code:

import random
import numpy as np

class Room:
    def __init__(self, name, contents):
        self.name = name
        self.contents = contents

rooms = np.zeros((10, 10))

emptyRooms = []

halfHeight = int(len(rooms[1]) / 2)
halfWidth = int(len(rooms[0]) / 2)

rooms[halfWidth][halfHeight] = 1

for r in range(len(rooms) - 1):
    for c in range(len(rooms) - 1):
        if rooms[r][c] == 1:
            if rooms[r][c-1] != 1:
                rooms[r][c-1] = 1
            if rooms[r][c 1] != 1:
                rooms[r][c 1] = 1
            if rooms[r-1][c] != 1:
                rooms[r-1][c] = 1
            if rooms[r 1][c] != 1:
                rooms[r 1][c] = 1

print(rooms)

Here I'm initializing the loop up to range(len(rooms) - 1) instead of range(len(rooms)). I hope it will resolve the issue.

CodePudding user response:

You have c 1, with c in range(len(rooms)): when c is the last one c 1 will be out of bounds.

CodePudding user response:

You have to check if r 1 or c 1 is smaller than ten, because than it tries to index an element, which does not exist (often also called out of bounds).

So in you code do:

import random
import numpy as np

class Room:
 def __init__(self, name, contents):
  self.name = name
  self.contents = contents

rooms = np.zeros((10, 10))

emptyRooms = []

halfHeight = int(len(rooms[1]) / 2)
halfWidth = int(len(rooms[0]) / 2)

rooms[halfWidth][halfHeight] = 1

for r in range(len(rooms)):
 for c in range(len(rooms)):
  if rooms[r][c] == 1:
   if (c-1) >= 0 and rooms[r][c-1] != 1:
    rooms[r][c-1] = 1
   if (c 1) < 10 and rooms[r][c 1] != 1:
    rooms[r][c 1] = 1
   if (r-1) >= 0 and rooms[r-1][c] != 1:
    rooms[r-1][c] = 1
   if (r 1) < 10 and rooms[r 1][c] != 1:
    rooms[r 1][c] = 1
print(rooms)

To make sure that at every position where you add 1 to either c or r the resulting integer is never greater than or equal to 10 (would always result in out of Bounds Error).

Edit: Probably you will also not want to get wrapping results in the negative direction. So i added also a check for the negative direction. (The Parts with '(r-1) >= 0'.)

  • Related