Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 1.37 KB

File metadata and controls

29 lines (21 loc) · 1.37 KB

Lab 7.03 - Kangaroo Class

Instructions

  • Finish writing the __add__ method for the time class from the Do Now.
  • Write a definition for a class named Kangaroo with the following methods:
    • An __init__ method that initializes an attribute named pouch_contents to an empty list.
    • A method named put_in_pouch that takes an object of any type and adds it to pouch_contents.
    • A __str__ method that returns a string representation of the Kangaroo object and the contents of the pouch.

Tips to give students

  • This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python
  • TypeError: Can't convert 'list' object to str implicitly
  • Use the str() function to convert the list object to a string.
  • Test your code by creating two Kangaroo objects
    • assign them to variables named kanga and roo
    • add roo to the contents of kanga’s pouch

Extra Credit

Return to your Pet class from Lab 7.02. Research the isinstance function to write a method, is_friend that will take in another pet and return True if the two pets are friends, and false if they are not.

Rules

  • If they are both dogs they are friends.
  • If the instance is a dog and the other pet is a cat, they are friends.
  • If the instance is a cat and the other is a dog they are not friends.
  • If they are both cats they are not friends.