最近在看《python入门经典》机械工业出版社,张敏译(好像python的书都是翻译过来的)p381页,运行代码清单13.6时,程序出错了,请专家们看看
附件1是英文版的书,电子版我只有英文版的,抱歉!
附件2是源程序
# -*- coding:utf-8 -*-
import random
class Island(object):
"""
Island nXn grid where zreo value indicates not occupied.
"""
def __init__(self, n, preyCnt=0, predatorCnt=0):
'''
Initialize grid to all 0's ,then fill with animals
'''
print n, preyCnt, predatorCnt
self.gridSize = n
self.grid = []
for i in range(n):
row = [0] * n
self.grid.append(row)
self.initAnimals(preyCnt, predatorCnt)
def animal(self,x,y):
'''
Return animal at location(x,y)
'''
if 0 <= x < self.gridSize and 0 <= y <= self.gridSize:
return self.grid[x][y]
else:
return -1 #outside island boundary
def initAnimals(self, preyCnt, predatorCnt):
'''
Put some intial animals on the island
'''
cnt = 0
while cnt < preyCnt:
x = random.randint(0, self.gridSize-1)
y = random.randint(1, self.gridSize-1)
if not self.animal(x, y):
newPrey = Prey(island=self , x=x, y=y )
cnt += 1
self.register(newPrey)
cnt = 0
while cnt < predatorCnt:
x = random.randint(0, self.gridSize-1)
y = random.randint(0, self.gridSize-1)
if not self.animal(x,y):
newPred = Predator(island=self , x=x, y=y )
cnt += 1
self.register(newPred)
def size(self):
'''
Return size fo the island,i.e. put it at the animal's coordinates
'''
return self.gridSize
def register(self,animal):
'''
Register animal with island, i.e. put it at the animal's coordinates
'''
x = animal.x
y = animal.y
self.grid[x][y] = animal
def __str__(self):
'''
String representation for printing.
(0,0) will be in the lower left corner
'''
s = ""
for j in range(self.gridSize-1, -1, -1):
for i in range(self.gridSize):
if not self.grid[j]:
s+= "%-2s" %'.' + " " #也可以写成s+="%-2s" %str('.') + " "
else:
s+= "%-2s" %(str(self.grid[j])) + " "
s+="\n"
return s
def remove(self, x, y):
self.grid[x][y] = 0
class Animal(object):
def __init__(self, island, x=0 , y=0, s="A" :
'''
Initialize the animal's and their positions
'''
self.island = island
self.name = s
self.x = x
self.y = y
def __str__(self):
return self.name
def position(self):
'''
Return coordinates of current position.
'''
return self.x, self.y
class Prey(Animal):
def __init__(self, island, x=0, y=0, s="O" :
Animal.__init__(self, island, x, y, s)
def move(self):
"""
Move to an open, neighboring pisition.
"""
offset = [(-1,1), (0,1), (1,1), (-1,0), (1,0), (-1,-1), (0,-1), (1,-1)]
for i in range(len(offset)):
x = self.x + offset[0]
y = self.y + offset[1]
if self.island.animal(x,y) == 0:
self.island.remove(self)
self.x = x
self.y = y
self.island.register(self)
break
class Predator(Animal):
def __init__(self, island, x=0, y=0, s="X" :
Animal.__init__(self, island, x, y, s)
if __name__ == '__main__':
royale = Island(10)
moose1 = Prey(island=royale, x=4, y=8, s='a1')
moose2 = Prey(island=royale, x=6, y=4, s='a2')
royale.register(moose1)
royale.register(moose2)
#moose1 = Prey(royale, 4, 8, "m1"
#moose2 = Prey(royale, 6, 4, "m2"
print royale
moose1.move()
moose2.move()
print royale![自学python中,求助下面的程序哪边出错,如何改?]()
错误信息.PNG |