-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcritter_zoo.py
More file actions
53 lines (45 loc) · 1.77 KB
/
critter_zoo.py
File metadata and controls
53 lines (45 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Critter Zoo
# A zoo containing a list of Critter objects.
# CONTAINMENT - one object containing other objects. In this context, our Zoo
# objects will contain Critter objects.
class Critter_zoo():
"""A zoo containing virtual Critter pets"""
def __init__(self, n, crits = None):
"""initializes a zoo with a name and a list of Critters"""
if crits == None:
crits = []
self._name = n
# the zoo maintains a list of all current critters at the zoo
self._critters = crits # list containing references to Critter objects
def get_crits(self):
"""returns the list of references to Critter objects"""
return self._critters
def get_name(self):
"""returns the name of the zoo"""
return self._name
def add_critter(self, crit):
"""adds a Critter object to the zoo"""
self._critters.append(crit)
# CHALLENGE: create a del_crit method to remove a Critter from the zoo and a
# find_crit method to return a reference to a Critter at the zoo
def find_critter(self, name):
"""locates a Critter at the zoo and returns a reference to it"""
for i in self._critters:
crit_name = i.get_name()
if crit_name == name:
return i
print("Critter is not at the zoo.")
def del_critter(self, name):
"""deletes a Critter object from the zoo"""
for i in range(len(critters)):
crit_name = self._critters[i].get_name()
if crit_name == name:
self._critters.pop(i)
print("Critter " + name + " is not at the zoo.")
def __str__(self):
"""string representation of a Critter zoo"""
string = "\nZoo: " + self._name
string += "\n" + str(len(self._critters)) + " Critters at the zoo..."
for i in self._critters:
string += "\n" + i.get_name()
return string