24小时热门版块排行榜    

查看: 674  |  回复: 1

lajiiwang

金虫 (小有名气)

[求助] 自学python中,求助下面的程序哪边出错,如何改?

最近在看《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
回复此楼

» 本帖附件资源列表

  • 欢迎监督和反馈:小木虫仅提供交流平台,不对该内容负责。
    本内容由用户自主发布,如果其内容涉及到知识产权问题,其责任在于用户本人,如对版权有异议,请联系邮箱:xiaomuchong@tal.com
  • 附件 1 : The_Practice_of_Computing_Using_Python.pdf
  • 2015-10-18 16:07:50, 9.52 M
  • 附件 2 : fs.py
  • 2015-10-18 16:11:00, 3.64 K

» 猜你喜欢

制冷计算
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

lajiiwang

金虫 (小有名气)

晚上11点半躺在床上脑子里面老是蹦出这个问题,突然有个新的想法,打开电脑输入代码,解决了。
看来书上的程序也不靠谱。
解决了这个问题,脑子里面关于类的概念又清晰里不少。
制冷计算
2楼2015-10-21 11:47:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 lajiiwang 的主题更新
信息提示
请填处理意见