【发布时间】:2016-06-18 21:37:04
【问题描述】:
我正在为一项作业编写黑盒测试代码。在我的函数中,输入是一个二维数组,x 为 int,y 为 int,僵尸强度为 int。如果元素的值小于僵尸强度,则从我的起始位置开始,我将元素的值更改为-1。比我得到的元素邻居,上、下、左、右(不是对角线)并对它们做同样的事情。我正在使用尝试,如果元素存在,则将其添加到列表中,如果不继续。据我了解,使用 try except 将停止添加列表中数组中不存在的元素。这就是为什么我不确定为什么我在黑盒测试代码时遇到索引错误的原因。我添加的输入,但我不知道黑盒测试使用的是什么输入。
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
【问题讨论】:
-
哪一行出现错误?
-
复制并粘贴完整的回溯到您的问题
-
我刚刚用 python 2.7 运行了你的代码,它运行良好
-
@Barmar 第 9 行给了我错误,这是我代码中唯一的 if 语句
-
您的代码中有 5 个
if语句。if x - 1 >= 0:、if y-1 >= 0:等
标签: python arrays runtime-error