dimanche 19 juin 2016

Not sure what is causing the index error in my if statement


I am writing code for a black box test for an assignment. In my function the inputs are a 2d array, and int for x, and int for y, and an int for zombie strength. From my start position if the value of the element is less than the zombie strength, I change the value of the element to -1. Than I got to that elements neighbors, up, down, left, and right (Not diagonal) and do the same to them. I am using a try except, if the element exists than add it to a list, if not move on. From what I understand using a try except will stop from adding element that do not exist in the array in the list. That is why I am not sure why I am getting an indexing error when I black box test my code. The inputs I added, but I do not know what inputs the black box test is using.

Here is my code 
population = [[9, 3, 4, 5, 4], [1, 6, 5, 8, 9], [2, 3, 7, 3, 2], [4,5,1,4,3], [4, 5, 4, 3, 9]]
x = 2
y = 1
zombie = 5



def answer(population, x, y, zombie):
    list = []
    list.append([x,y])
    while list:
        print list
        front_of_list = list.pop(0)
        x = front_of_list[0]
        y = front_of_list[1]
        if population[front_of_list[0]][front_of_list[1]] <= zombie and population[front_of_list[0]][front_of_list[1]] >= 0:
            population[front_of_list[0]][front_of_list[1]] =  -1
            if x-1 >=0:
                try:
                    population[x-1][y]
                    list.append([x-1,y])
                except:
                    pass
            if y-1 >= 0:
                try:
                    population[x][y-1]
                    list.append([x,y-1])
                except:
                    pass
            if x+1 < len(population):
                try:
                    population[x+1][y]
                    list.append([x+1,y])
                except:
                    pass
            if y+1 < len(population[0]):
                try:
                    population[x][y+1]
                    list.append([x,y+1])
                except:
                    pass



answer(population, x, y, zombie)
print population

Aucun commentaire:

Enregistrer un commentaire