【问题标题】:Not sure what is causing the index error in my if statement不确定是什么导致我的 if 语句中的索引错误
【发布时间】: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 &gt;= 0:if y-1 &gt;= 0:

标签: python arrays runtime-error


【解决方案1】:

您的try 子句不是必需的,因为您之前进行的if 测试涵盖了这种情况。让我们尝试简化和澄清代码,看看是否能解决您的问题,因为我们无法直接重现它。

首先,当xy 可以使用时,您继续使用front_of_list;我们可以通过组合测试来简化if 子句(Python 特性); (x, y) 自然是元组,所以我切换了它们;您重新定义了内置名称list,所以我将其切换为array;最后我将测试减少到循环,以减少复制粘贴错误的机会。这给我们留下了:

def answer(population, x, y, zombie):
    array = [(x, y)]

    x_limit = len(population)
    y_limit = len(population[0])

    while array:
        print array
        x, y = array.pop(0)

        if 0 <= population[x][y] <= zombie:

            population[x][y] = -1

            for dx in (-1, 1):
                if 0 <= x + dx < x_limit:
                    array.append((x + dx, y))

            for dy in (-1, 1):
                if 0 <= y + dy < y_limit:
                    array.append((x, y + dy))

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, y = 2, 1

zombie = 5

answer(population, x, y, zombie)

print population

将其插入您的测试情况,并在它继续失败时传递确切的错误消息。

您的代码假设一个矩形矩阵,如果您的二维数组参差不齐,这将导致索引错误。 (需要仔细检查。)

更新

传统意义上的 x 是 columns 而 y 是 rows 但您的代码(以及我的返工)却反过来了。在内部,这没有什么区别,但是由于您被传递 x & y 作为参数,从您插入的代码的角度来看,这可能意味着起点不正确,并可能导致它使您的结果无效。 (再次,需要仔细检查。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 2016-08-10
    相关资源
    最近更新 更多