8000 added exercise code · smpotts/think-python@ac224e5 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit ac224e5

Browse files
committed
added exercise code
1 parent 6d39223 commit ac224e5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/kangaroo.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Kangaroo:
2+
3+
def __init__(self, name, pouch_contents=None):
4+
# __init__ runs, it checks the value of contents and,
5+
# if necessary, creates a new empty list. That way,
6+
# every Kangaroo that gets the default value gets a
7+
# reference to a different list.
8+
9+
self.name = name
10+
11+
if pouch_contents is None:
12+
pouch_contents = []
13+
self.pouch_contents = pouch_contents
14+
15+
def put_in_pouch(self, obj):
16+
self.pouch_contents.append(obj)
17+
18+
def __str__(self):
19+
t = [self.name + ' has pouch contents:']
20+
for thing in self.pouch_contents:
21+
s = ' ' + object.__str__(thing)
22+
t.append(s)
23+
return '\n'.join(t)

tests/test_kangaroo.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from src.kangaroo import Kangaroo
3+
4+
5+
class TestKangaroo(unittest.TestCase):
6+
def test_initialization(self):
7+
kangaroo = Kangaroo("kangaroo")
8+
self.assertEqual(kangaroo.pouch_contents, [])
9+
10+
def test_kangaroo_str(self):
11+
kangaroo = Kangaroo("kangaroo")
12+
kangaroo.put_in_pouch("hello")
13+
kangaroo.put_in_pouch("world")
6DB1 14+
print(kangaroo)
15+
16+
def test_put_in_pouch(self):
17+
kanga = Kangaroo("kanga")
18+
roo = Kangaroo("roo")
19+
kanga.put_in_pouch(roo)
20+
print(kanga)
21+
22+
23+
if __name__ == '__main__':
24+
unittest.main()

0 commit comments

Comments
 (0)
0