Interactive Computer Science Tutoring

Learn programming concepts interactively

AHA Schools Logo

Python Classes

Master object-oriented programming through interactive pet creation

Learning Modes & DifficultyInteractive
Choose your learning focus and progression level

Difficulty Level

Learning Focus

Class Blueprint: PetTemplate
A class is like a blueprint or template for creating objects

Attributes (Data)

self.name(string)
self.species(string)
self.age(integer)
self.hunger(0-100)
self.happiness(0-100)
self.energy(0-100)

Methods (Behavior)

__init__()(constructor)
feed()(reduces hunger)
play()(increases happiness)
sleep()(restores energy)
get_status()(returns info)

What is a Class?

A class is like a blueprint for building houses. Just as you can build many houses from one blueprint, you can create many pet objects from one Pet class. Each pet will have the same structure (attributes) and capabilities (methods), but different values.

Global Actions
Python Classes Code
Live Python class definition with highlighted changes
# === PYTHON CLASSES INTERACTIVE DEMO ===
class Pet:
"""A class representing a virtual pet"""
def __init__(self, name, species, age):
"""Constructor - called when creating new pet"""
self.name = name
self.species = species
self.age = age
self.hunger = 50
self.happiness = 75
self.energy = 80
self.is_alive = True
def feed(self, amount=20):
"""Feed the pet to reduce hunger"""
self.hunger = max(0, self.hunger - amount)
self.happiness = min(100, self.happiness + 10)
return f"{self.name} has been fed!"
def play(self, amount=20):
"""Play with pet to increase happiness"""
self.happiness = min(100, self.happiness + amount)
self.energy = max(0, self.energy - 15)
return f"{self.name} had fun playing!"
def sleep(self, amount=20):
"""Let pet sleep to restore energy"""
self.energy = min(100, self.energy + amount)
return f"{self.name} is well rested!"
def get_status(self):
"""Get current pet status"""
return {
"name": self.name,
"species": self.species,
"age": self.age,
"hunger": self.hunger,
"happiness": self.happiness,
"energy": self.energy
}
# === CURRENT PET INSTANCES ===
pet1 = Pet("Buddy", "dog", 3)
# Current pet states:
# Buddy: Hunger=30, Happiness=80, Energy=70
History0/5
Recent class operations with timestamps

No operations yet

Start by adding or modifying elements

Learning Progress

Class Definition
Object Instantiation
Constructor Method
Completed: 0 / 10