Wowzz 发表于 2019-8-19 18:33:25

零基础入门python 报错问错

这段代码报错:输入
>>> pool=Pool(1,19)
>>> pool.print_num()
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
    pool.print_num()
File "E:/python/test1.py", line 15, in print_num
    print("水池里总共有乌龟%d只,小鱼%d条!"%(self.trutle.num,self.fish.num))
AttributeError: 'Pool' object has no attribute 'trutle'
>>>


源码如下:


class Trutle:
    def __init__(self,x):
      self.num=x

class Fish:
    def __init__(self,x):
      self.num=x

class Pool:
    def __init__(self,x,y):
      self.trurle=Trutle(x)
      self.fish=Fish(y)

    def print_num(self):
      print("水池里总共有乌龟%d只,小鱼%d条!"%(self.trutle.num,self.fish.num))
            

zltzlt 发表于 2019-8-19 18:36:13

class Trutle:
    def __init__(self,x):
      self.num=x

class Fish:
    def __init__(self,x):
      self.num=x

class Pool:
    def __init__(self,x,y):
      self.trurle=Trutle(x)
      self.fish=Fish(y)

    def print_num(self):
      print("水池里总共有乌龟%d只,小鱼%d条!"%(self.trutle.num,self.fish.num))

我标红的地方应该改为 trutle

DT_Nelson 发表于 2019-8-19 18:51:36

emm,你应该知道乌龟的英文单词是turtle不是trutle的吧……
页: [1]
查看完整版本: 零基础入门python 报错问错