Learn programming concepts interactively
self.name
(string)self.species
(string)self.age
(integer)self.hunger
(0-100)self.happiness
(0-100)self.energy
(0-100)__init__()
(constructor)feed()
(reduces hunger)play()
(increases happiness)sleep()
(restores energy)get_status()
(returns info)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.
# === 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 = nameself.species = speciesself.age = ageself.hunger = 50self.happiness = 75self.energy = 80self.is_alive = Truedef 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
No operations yet
Start by adding or modifying elements