# I keep getting the 'infinite loop' error

I’m trying to do Gridmancer Redux in Python, but it keeps giving me the ‘infinite loop’ error, every single time at exactly: 12.5.
My code:

``````def findFirstCoin ():
exampleCoin = hero.navGrid[0][0]
arrNavG = hero.navGrid
for i in range(0,20):
for j in range(0,20):
result = arrNavG[i][j]
if hero.navGrid[i][j] == 'Coin': return i,j
return 99999,99999
def findRectangleSize (grid, x, y):
maxXY = 20
contX = True
contY = True
i = 0
j = 0
width = 0
height = 0
if (not grid[x+i]) or (not grid[x+i][y+j]): return
while grid[x+i][y+j]=='Coin':
width += 1
i += 1
if (not grid[x+i]) or (not grid[x+i][y+j]): break
hero.wait(0.1);
for j in range(0, (maxXY - y) + 1):
for i in range(0, (width + 1)):
if i == width - 1 and grid[x+i][y+j] == 'Coin':
newwidth = i
break
elif grid[x+i][y+j] == 'Coin': newwidth = i
else: return newwidth + 1, j
return newwidth+1,j
arrNavG=hero.navGrid
hero.say(arrNavG[18][4])
while xFirst != 99999:
firstCoinPos = findFirstCoin()
xFirst = firstCoinPos[0]
yFirst = firstCoinPos[1]
grid = hero.navGrid
rectSize = findRectangleSize(grid, xFirst, yFirst)
wide = rectSize[0]
high = rectSize[1]
``````

You can also answer with Javascript if you want.

New code:

``````def findFirstCoin ():
exampleCoin = hero.navGrid[0][0]
arrNavG = hero.navGrid
for i in range(0,20):
for j in range(0,20):
result = arrNavG[i][j]
if hero.navGrid[i][j] == 'Coin': return i,j
return 99999,99999
def findRectangleSize (grid, x, y):
if not grid: return
maxXY = 20
contX = True
contY = True
i = 0
j = 0
width = 0
height = 0
if (not grid[x+i]) or (not grid[x+i][y+j]): return
while grid[x+i][y+j]=='Coin':
width += 1
i += 1
if (not grid[x+i]) or (not grid[x+i][y+j]): break
hero.wait(0)
findRectangeSize()
for j in range(0, (maxXY - y) + 1):
for i in range(0, (width + 1)):
if i == width - 1 and grid[x+i][y+j] == 'Coin':
newwidth = i
break
elif grid[x+i][y+j] == 'Coin': newwidth = i
else: return newwidth + 1, j
return newwidth+1,j
arrNavG=hero.navGrid
hero.say(arrNavG[18][4])
while xFirst != 99999:
firstCoinPos = findFirstCoin()
xFirst = firstCoinPos[0]
yFirst = firstCoinPos[1]
grid = hero.navGrid
rectSize = findRectangleSize(grid, xFirst, yFirst)
wide = rectSize[0]
high = rectSize[1]