- Page ID
Download the code from this chapter from http://thinkpython2.com/code/Time2.py. Change the attributes of
Time to be a single integer representing seconds since midnight. Then modify the methods (and the function
int_to_time) to work with the new implementation. You should not have to modify the test code in
main. When you are done, the output should be the same as before.
This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python. Write a definition for a class named
Kangaroo with the following methods:
__init__method that initializes an attribute named
pouch_contentsto an empty list.
- A method named
put_in_pouchthat takes an object of any type and adds it to
__str__method that returns a string representation of the Kangaroo object and the contents of the pouch.
Test your code by creating two
Kangaroo objects, assigning them to variables named
roo, and then adding
roo to the contents of
Download http://thinkpython2.com/code/BadKangaroo.py. It contains a solution to the previous problem with one big, nasty bug. Find and fix the bug.
If you get stuck, you can download http://thinkpython2.com/code/GoodKangaroo.py, which explains the problem and demonstrates a solution.