avatar Bite 138. OOP fun at the Zoo

Finish the Animal class below adding one or more class variables and a classmethod so that the following code:

dog = Animal('dog')
cat = Animal('cat')
fish = Animal('fish')
lion = Animal('lion')
mouse = Animal('mouse')
print(Animal.zoo())

... produces the following output:

10001. Dog
10002. Cat
10003. Fish
10004. Lion
10005. Mouse

Few things to note here:

  • The sequencing starts at 10000,
  • Each animal gets title cased,
  • An individual animal should print the sequence+name string as well, so best to implement the __str__ method on the class.

So making another animal at this point, the following should work:

horse = Animal('horse')
assert str(horse) == "10006. Horse"

As usual this is what the pytest code tests when you submit your code.

Have fun and code more Python! Join our thriving Slack Community under Settings to learn together with other passionate Pythonistas ...

Login and get coding
go back Intermediate level
Bitecoin 3X

279 out of 281 users completed this Bite.
Will you be Pythonista #280 to crack this Bite?
Resolution time: ~47 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 5.15 on a 1-10 difficulty scale.
» Up for a challenge? 💪

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help