Feedback: Danger Valley

So, in python, it’s a bit more complicated.

for element in array:
    # element is a[0], a[1], a[2], ..., a[len(array)-1]
for index in range(len(hero.grid)):
    # index is 0, 1, 2, ..., len(array) - 1

The pythonic way of writing the function would be: (which I didn’t understand while writing the level)

# Iterate over each row
for row in hero.grid:
    # Iterate over each cell in a row
    for cell in row:
        hero.say(cell) # 1 or 0

But the traditional for-loop way with indexii is:

# Iterate over each index of the parent array:
for rowIndex in range(len(hero.grid)):
    row = hero.grid[rowIndex]
    # Iterate over each index of the nested array:
    for cellIndex in range(len(row)):
        cell = row[cellIndex]
        hero.say(cell) # 1 or 0

Edit, now finally, what you are saying is:

for i in range(len(hero.grid)):
    row = hero.grid[i]
    for j in hero.grid[i]:
        # hero.grid[i as index (number)][j as element (a number, but only 0 or 1)]
        if hero.grid[i][j] == 1:
            # do stuff
1 Like