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 ,看了幫助才知道正確的寫法

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容