Python  8.a  methodical   approach

add a method description,to your Animal class.using two separate print statements,it should print out the name and age of the animal it's called on.then,create an instance of Animal,hippo(with whatever name and age you like),and can its description method.

Hint:
your method should look something like this:
def description(self):
print self.name
print self.age
after that all you need to do is create a hippo and call its description method with hippo.description()

remember to pass self as an argument to description.otherwise,printing self.name and self.age won't work,since python won't know which self (that is,which object) you're talking about.

class Animal (object):
""" makes cute animals """
is_alive = True
def init(self,name,age):
self.name = name
self.age = age
#Add your method here!
def description(self):
print self.name
print self.age
hippo = Animal("chad",7)
hippo.description()

两个def要对齐,不然就出错。

using two separate print ,看了帮助才知道正确的写法

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容