私は割り当てのためのブラックボックステスト用のコードを書いています。私の関数では、入力は2次元配列、xはint、yはint、ゾンビ強度はintです。要素の値がゾンビの強さよりも小さい場合、私の開始位置から、要素の値を-1に変更します。私はその要素の上下、左、右(斜めではない)に近づき、同じことをするよりも。私は、要素が存在していない場合は、それをリストに追加するよりも存在する場合を除いてtryを使用しています。私がtryを使用して理解していることから、リストに配列に存在しない要素を追加することを止めます。なぜ私はブラックボックスが私のコードをテストするときにインデックスエラーをなぜ得ているのかわかりません。私が追加した入力は、ブラックボックステストがどの入力を使用しているのかわかりません。あなたがif
テストは前の状況をカバーして私のif文で何がインデックスエラーを引き起こしているのかわからない
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
どのラインがエラーになっていますか? – Barmar
あなたの質問に完全なトレースバックをコピーして貼り付けてください –
私はPython 2.7でコードを実行しましたが、それは正常に実行されました –