Gridmancer Redux 'cannot read property 10'

Hi,

I’m trying to do the Gridmancer Redux level, but my code keeps stopping when it tries to put blocks in at the top row (19) as if there is an out of bounds issue (but this row exists so I don’t understand what the issue is)

Here is 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]
            #hero.say('hey '+result)
            #hero.say(hero.navGrid[i][j])
            if hero.navGrid[i][j]=='Coin':
                #hero.say('There is a coin at i:'+i + ' j: ' +j)
                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
    hero.say("Pos ("+(x+i)+","+(y+j)+") contains: " + grid[x+i][y+j])
    while grid[x+i][y+j]=='Coin':
        width+=1
        i+=1
    #hero.say('orig width '+width)
    
    for j in range(0,(maxXY-y)+1):
        for i in range(0,(width+1)):
            #hero.say("Pos ("+i+","+j+") contains: " + grid[x+i][y+j])
            if i==width-1 and grid[x+i][y+j]=='Coin':
                #hero.say('i'+i+' j ' + j + ' about to break')
                newwidth=i
                break
            elif grid[x+i][y+j]=='Coin':
                newwidth=i
            elif grid[x+i][y+j]!='Coin':
                #hero.say('No coin at '+x+i+','+y+j+' '+grid[x+i][y+j])
                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]
    #hero.say('The first coin is at pos {'+xFirst+'},{'+yFirst+'}')
    grid=hero.navGrid
    
    rectSize=findRectangleSize(grid,xFirst,yFirst)
    wide=rectSize[0]
    high=rectSize[1]
    #hero.say('X:'+xFirst+'Y:'+yFirst+ '{'+wide+'},{'+high+'}')
    hero.addRect(xFirst, yFirst, wide, high)

You forgot to put a return statement before the loop and a break statement in the loop.
Hope it works now. :grin:

Hi. Thanks for the feedback, which function is missing the return statement? Also when a return statement is hit doesn’t that end the function (both functions have return statements so I’m not sure which one you mean has an issue).

The function where there is an error of course. :grin:

And yes, a return statement does end the function.