24小时热门版块排行榜    

查看: 719  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 材料专硕找调剂 +3 哈哈哈吼吼吼哈 2026-03-23 3/150 2026-03-23 23:13 by peike
[考研] 335分 | 材料与化工专硕 | GPA 4.07 | 有科研经历 +4 cccchenso 2026-03-23 4/200 2026-03-23 23:00 by 徐ckkk
[考研] 303求调剂 +4 元夕元 2026-03-20 4/200 2026-03-23 19:00 by macy2011
[考研] 328求调剂,英语六级551,有科研经历 +7 生物工程调剂 2026-03-17 12/600 2026-03-23 18:18 by YMU施老师
[考研] 070300,一志愿北航320求调剂 +3 Jerry0216 2026-03-22 5/250 2026-03-23 09:16 by 。。堂堂
[考研] 276求调剂 +3 YNRYG 2026-03-21 4/200 2026-03-23 08:31 by 醉在风里
[考研] 一志愿070300浙大化学358分,求调剂! +4 酥酥鱼.. 2026-03-21 4/200 2026-03-23 08:12 by Iveryant
[考研] 298求调剂一志愿211 +3 上岸6666@ 2026-03-20 3/150 2026-03-22 15:50 by ColorlessPI
[考研] 305分求调剂(食品工程) +4 Sxy112 2026-03-21 6/300 2026-03-22 15:26 by 无懈可击111
[考研] 260求调剂 +3 朱芷琳 2026-03-20 4/200 2026-03-22 15:12 by 朱芷琳
[考研] 求调剂 +7 Auroracx 2026-03-22 7/350 2026-03-22 12:38 by 素颜倾城1988
[考研] 考研调剂 +4 来好运来来来 2026-03-21 4/200 2026-03-22 12:15 by 星空星月
[考研] 一志愿东华大学控制学硕320求调剂 +3 Grand777 2026-03-21 3/150 2026-03-21 19:23 by 简之-
[考研] 一志愿南大,0703化学,分数336,求调剂 +3 收到VS 2026-03-21 3/150 2026-03-21 18:42 by 学员8dgXkO
[考研] 一志愿天津大学化学工艺专业(081702)315分求调剂 +12 yangfz 2026-03-17 12/600 2026-03-21 03:30 by JourneyLucky
[考研] 一志愿重庆大学085700资源与环境专硕,总分308求调剂 +3 墨墨漠 2026-03-18 3/150 2026-03-21 00:39 by JourneyLucky
[考研] 295求调剂 +4 一志愿京区211 2026-03-18 6/300 2026-03-20 23:41 by JourneyLucky
[考研] 308求调剂 +3 阿姐阿姐家啊 2026-03-18 3/150 2026-03-20 23:24 by JourneyLucky
[考研] 求调剂一志愿南京航空航天大学289分 +3 @taotao 2026-03-19 3/150 2026-03-20 21:34 by JourneyLucky
[考研] 一志愿西南交通 专硕 材料355 本科双非 求调剂 +5 西南交通专材355 2026-03-19 5/250 2026-03-20 21:10 by JourneyLucky
信息提示
请填处理意见