Project: my pet
Section 12: Classes and objects (class)Lesson 12.2 · ⏱ 12 min
🎯 Project goal
A cat called Mishmish, with hunger and happiness. We feed her and play with her, and her state and mood change.
max(0, ...)stops the hunger going below zero.min(10, ...)stops the happiness going above 10.- The mood is worked out from the happiness with a condition on one line.
class Pet:
def __init__(self, name):
self.name = name
self.hunger = 5
self.happy = 5
def feed(self):
self.hunger = max(0, self.hunger - 3)
def play(self):
self.happy = min(10, self.happy + 2)
self.hunger += 1
def status(self):
mood = "happy" if self.happy >= 7 else "ok" if self.happy >= 4 else "sad"
print(self.name, "| hunger:", self.hunger, "| happy:", self.happy, "|", mood)
cat = Pet("Mishmish")
cat.status()
cat.play()
cat.play()
cat.status()
cat.feed()
cat.status()
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Mishmish | hunger: 5 | happy: 5 | ok Mishmish | hunger: 7 | happy: 9 | happy Mishmish | hunger: 4 | happy: 9 | happy

- Add a
sleepfunction that adds 1 to happiness and 2 to hunger. - Make a second pet, make the two play, and print their state.
📌 Another example: two pets
Two objects of the same class, each with its own hunger.
class Pet:
def __init__(self, name):
self.name = name
self.hunger = 5
def feed(self):
self.hunger = max(0, self.hunger - 3)
print(self.name, "hunger:", self.hunger)
a = Pet("Mishmish")
b = Pet("Bondo")
a.feed()
a.feed()
b.feed()
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Mishmish hunger: 2 Mishmish hunger: 0 Bondo hunger: 2
✍️ Practise
Predict Mishmish's state after feed and then play three times, then try it to check.
✍️ Practise here in the lesson✖ Close the labor open the lab in a new page ↗
🎯 Challenge yourself
Easy
Add a play function that adds 2 to happy.
👀 Show the solution🙈 Hide the solution
class Pet:
def __init__(self, name):
self.name = name
self.happy = 5
def play(self):
self.happy = self.happy + 2
print(self.name, "happy:", self.happy)
Pet("Bondo").play()
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Bondo happy: 7
Result: Bondo happy: 7
Medium
A status function that prints the pet's state: hungry if the hunger is more than 5.
👀 Show the solution🙈 Hide the solution
class Pet:
def __init__(self, name, hunger):
self.name = name
self.hunger = hunger
def status(self):
if self.hunger > 5:
print(self.name, "is hungry")
else:
print(self.name, "is fine")
Pet("Mishmish", 8).status()
Pet("Bondo", 2).status()
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Mishmish is hungry Bondo is fine
Result: Mishmish is hungry and Bondo is fine
Hard
A list of pets, and feed them all with a loop.
👀 Show the solution🙈 Hide the solution
class Pet:
def __init__(self, name):
self.name = name
self.hunger = 6
def feed(self):
self.hunger = self.hunger - 3
print(self.name, self.hunger)
pets = [Pet("Mishmish"), Pet("Bondo"), Pet("Lulu")]
for p in pets:
p.feed()
🖥️ The output in Robo Python:
🐍 Open the code in Robo Python ↗Mishmish 3 Bondo 3 Lulu 3
Result: each one's hunger became 3.

