0

Here is the code:

import random
import numpy as np

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

rooms = np.zeros((11, 11))
maxRooms = 7
possibleNextRoom = []

def resetLevel():
  global rooms

  for r in range(len(rooms[0])):
    for c in range(len(rooms[1])):
      rooms[r][c] = 0

  possibleNextRoom = []

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

def countRooms():
  global rooms

  roomCount = 0

  for r in range(len(rooms)):
    for c in range(len(rooms)):
      if rooms[r][c] == 1:
        roomCount += 1

  return roomCount

def findPossibleRooms():

  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:
          possibleNextRoom.append((r, c+1))
        if rooms[r][c-1] != 1:
          possibleNextRoom.append((r, c-1))
        if rooms[r-1][c] != 1:
          possibleNextRoom.append((r-1, c))
        if rooms[r+1][c] != 1:
          possibleNextRoom.append((r+1, c))

def addRoom():

  nextRoom = random.randrange(0, len(possibleNextRoom))
  rooms[possibleNextRoom[nextRoom][0]][possibleNextRoom[nextRoom][1]] = 1
  possibleNextRoom.pop(nextRoom)

def generateLevel():
  resetLevel()

  while countRooms() < maxRooms:
    countRooms()
    findPossibleRooms()
    addRoom()

def displayLevel():
  print(rooms)

generateLevel()
displayLevel()

Here is the Error:

ValueError: empty range for randrange() (0, 0, 0)

I thought I was using random correctly, but appearantly not. I tried making the array start with something in it, but it still gave me the same error. I have been working on this for a long time and its giving me a headache. Any help with this is greatly appreciated. The error is on line 54.

  • On which line is the error? – Leo Nov 04 '21 at 02:14
  • 1
    the error is on line 54 @Leo – Jedidiah Blitz Nov 04 '21 at 02:17
  • Could possibleNextRoom be empty? Randint raises that error when the first and second value are 0 – Leo Nov 04 '21 at 02:21
  • 1
    Try printing `possibleNextRoom` right before you call that function. You'll see that it is empty. – ddejohn Nov 04 '21 at 02:22
  • Does this answer your question? [Python - empty range for randrange() (0,0, 0) and ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))](https://stackoverflow.com/questions/18161513/python-empty-range-for-randrange-0-0-0-and-valueerrorempty-range-for-ra) – Leo Nov 04 '21 at 02:22
  • 1
    That works, but now it gives me TypeError: 'int' object is not subscriptable on line 56 – Jedidiah Blitz Nov 04 '21 at 02:24
  • 2
    Reason #31857 why you don't want to fiddle with global variables. You really need to learn how to use functions properly by passing arguments and returning results, because this type of programming leads to exactly this type of issue: really hard to find bugs. – ddejohn Nov 04 '21 at 02:25
  • That error means that you are trying to do a list/dictionary operation on a integer – Leo Nov 04 '21 at 02:27
  • 1
    I know what it means, but rooms and possibleNextRoom are both arrays @Leo – Jedidiah Blitz Nov 04 '21 at 02:28
  • If you define or redifine a variable inside a function, the global variable is unchanged. So use "global variable" to make it change the big thing. – Leo Nov 04 '21 at 02:32
  • so i just add "global possibleNextRoom" in the function? @Leo – Jedidiah Blitz Nov 04 '21 at 02:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 04 '21 at 11:11

1 Answers1

0
In [90]: import random
In [91]: random.randrange?
Signature: random.randrange(start, stop=None, step=1, _int=<class 'int'>)
Docstring:
Choose a random item from range(start, stop[, step]).
...

This produces your error:

In [92]: random.randrange(0,0)
Traceback (most recent call last):
  File "<ipython-input-92-566d68980a89>", line 1, in <module>
    random.randrange(0,0)
  File "/usr/lib/python3.8/random.py", line 226, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0, 0, 0)

So if the error occurs in (telling us the line number if 52 is nearly useless)

nextRoom = random.randrange(0, len(possibleNextRoom))

it means possibleNextRoom is an empty list, [] (or possibly something else with a 0 len).

I see a

possibleNextRoom = []

The append in findPossibleRooms operate in-place, so can modify this global list. But they are wrapped in for and if, so I can't say whether they run at all.

In any case, the error tells that nothing has been appended, for one reason or other.

hpaulj
  • 221,503
  • 14
  • 230
  • 353