class Island (object):
def __init__(self, i,j,k, wolf_count=0, eagle_count=0, rabbit_count=0, pigeon_count=0,):
'''Initialize grid to all 0's, then fill with animals
'''
# print(n,prey_count,predator_count)
self.i=i
self.j=j
self.k=k
self.cube= []
for k in range(k):
self.square=[]
for j in range(j):
row=[0]*i
self.square.append(row)
self.cube.append(self.square)
self.init_animals(wolf_count, eagle_count, rabbit_count, pigeon_count)
def init_animals(self,wolf_count, eagle_count, rabbit_count, pigeon_count):
count = 0
while count < wolf_count:
i = random.randint(0,self.i-1)
j = random.randint(0,self.j-1)
k = 0
if not self.animal(i,j,k):
new_Wolf=Wolf(island=self,i=i,j=j,k=0)
count += 1
self.register(new_Wolf)
def animal(self,i,j,k):
'''Return animal at location (i,j,k)'''
if 0 <= i < self.i and 0 <= j < self.j and 0 <= k < self.k:
return self.cube[i][j][k]
else:
return -1
这些是我的程序中相互调用的部分。当我尝试运行程序时,它给了我:
IndexError: list index out of range.
它说的是return self.cube[i][j][k]
in animal()
。参考 中的 if not self.animal(i,j,k):
部分init_animals()
。这再次参考isle = Island(i,j,k, initial_Wolf, initial_Pigeon, initial_Eagle, initial_Rabbit)
.__init__()
知道为什么我会收到此错误吗?抱歉,如果它难以阅读。