Lesson 29 of 30
Lesson 29 ? Build a tiny pet game
Title: Feed, play, and care for a virtual pet
Description: Combine dictionaries, functions, loops, and choices in one small pet game.
Why it matters for kids: Projects help you see how small Python ideas become one fun program.
1. Create the pet
pet = {
"name": "Mochi",
"hunger": 5,
"happiness": 5,
}
print(pet)2. Feed the pet
pet = {
"name": "Mochi",
"hunger": 5,
}
pet["hunger"] = pet["hunger"] - 1
print(f"{pet['name']} has hunger {pet['hunger']}.")3. Use a function
def feed_pet(pet):
pet["hunger"] = pet["hunger"] - 1
print(f"You fed {pet['name']}.")
pet = {"name": "Luna", "hunger": 5}
feed_pet(pet)
print(pet)4. Add a menu
pet = {"name": "Mochi", "hunger": 5, "happiness": 5}
choice = input("feed or play? ")
if choice == "feed":
pet["hunger"] = pet["hunger"] - 1
elif choice == "play":
pet["happiness"] = pet["happiness"] + 1
else:
print("Mochi tilts their head.")
print(pet)5. Easy examples
pet = {"name": "Bean", "energy": 3}
pet["energy"] = pet["energy"] + 2
print(pet)def show_pet(pet):
print(f"Name: {pet['name']}")
print(f"Hunger: {pet['hunger']}")
show_pet({"name": "Pip", "hunger": 4})actions = ["feed", "play", "sleep"]
for action in actions:
print("Action:", action)6. Mini challenge
Click each example to open it.
Example 1
Create a pet dictionary.
Example 2
Print the pet name.
Example 3
Print pet hunger.
Example 4
Feed the pet by lowering hunger.
Example 5
Play with the pet by raising happiness.
Example 6
Create a feed function.
Example 7
Create a play function.
Example 8
Show a menu with feed and play.
Example 9
Add a sleep choice.
Example 10
Print the pet status after each choice.