24小时热门版块排行榜    

Znn3bq.jpeg
查看: 733  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 药学求调剂 +12 喽哈加油 2026-04-14 14/700 2026-04-15 23:49 by 陈皮皮
[考研] 求调剂学校 +14 不会吃肉 2026-04-13 16/800 2026-04-15 21:59 by noqvsozv
[考研] 药学求调剂 +11 RussHu 2026-04-12 13/650 2026-04-15 19:07 by zhuwenxu
[考研] 290调剂生物0860 +37 哇哈哈,。 2026-04-11 43/2150 2026-04-15 18:55 by zhuwenxu
[考研] 291求调剂 +7 关忆北. 2026-04-14 7/350 2026-04-15 14:39 by 黑科技花岗岩
[考研] 0854调剂 +13 长弓傲 2026-04-12 16/800 2026-04-15 13:45 by fenglj492
[考研] 26药学专硕105500求调剂 +6 喽哈加油 2026-04-13 6/300 2026-04-14 16:40 by zhouxiaoyu
[考研] 材料专业344求调剂 +17 hualkop 2026-04-10 22/1100 2026-04-14 16:21 by sxdj2
[考研] 本科西工大 324求调剂 +5 wysyjs25 2026-04-10 5/250 2026-04-13 23:08 by pies112
[考研] 0831一轮调剂失败求助 +10 小熊睿睿_s 2026-04-11 10/500 2026-04-12 22:43 by 长弓傲
[考研] 电气工程专硕320求调剂 +5 小麻子111 2026-04-10 5/250 2026-04-12 10:47 by zhouyuwinner
[考研] 一志愿华中农微生物,288分,三年实验经历 +11 代fish 2026-04-09 11/550 2026-04-12 10:21 by Hayaay
[考研] 086003调剂求助 +21 苏弋万 2026-04-09 22/1100 2026-04-11 20:25 by dongdian1
[考研] 283求调剂 086004考英二数二 +17 那个噜子 2026-04-10 18/900 2026-04-11 16:27 by 明月此时有
[考研] 机械专硕270求调剂,接受跨专业 +12 老师看看我吧aba 2026-04-09 14/700 2026-04-11 10:21 by laoshidan
[考研] 农业管理302分求调剂 +3 xuening1 2026-04-10 3/150 2026-04-11 10:18 by zhq0425
[考研] 一志愿北理工298英一数二已上岸,感谢各位老师 +14 Reframe 2026-04-10 16/800 2026-04-10 23:07 by caotw2020
[考研] 青岛科技大学材料学院,环境学院调剂补录4月10日以前都可以 +3 1青科大。 2026-04-09 5/250 2026-04-10 09:58 by 翩翩一书生
[考研] 一志愿中科大070300化学,314分求调剂 +12 wakeluofu 2026-04-09 12/600 2026-04-10 09:57 by liuhuiying09
[考研] 085601初试330分找调剂 +10 流心奶黄包l 2026-04-09 10/500 2026-04-10 08:14 by Sammy2
信息提示
请填处理意见