انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

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:
Mishmish | hunger: 5 | happy: 5 | ok
Mishmish | hunger: 7 | happy: 9 | happy
Mishmish | hunger: 4 | happy: 9 | happy
🐍 Open the code in Robo Python ↗
Mishmish's state after playing and eating
Mishmish's state after playing and eating
  1. Add a sleep function that adds 1 to happiness and 2 to hunger.
  2. 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.

🐍 Two pets
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:
Mishmish hunger: 2
Mishmish hunger: 0
Bondo hunger: 2
🐍 Open the code in Robo Python ↗
✍️ Practise
Predict Mishmish's state after feed and then play three times, then try it to check.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Add a play function that adds 2 to happy.

👀 Show the solution
🐍 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:
Bondo happy: 7
🐍 Open the code in Robo Python ↗

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
🐍 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:
Mishmish is hungry
Bondo is fine
🐍 Open the code in Robo Python ↗

Result: Mishmish is hungry and Bondo is fine

Hard

A list of pets, and feed them all with a loop.

👀 Show the solution
🐍 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:
Mishmish 3
Bondo 3
Lulu 3
🐍 Open the code in Robo Python ↗

Result: each one's hunger became 3.

Finished the lesson? Mark it so your progress counts.