Hoppy has a grand vision: a Self-Sustaining Digital Forest where magic tends to itself. To achieve this, we need more than just spells; we need workers.
Welcome to The Golem Project.
Over the next 5 levels, we will build a fully autonomous magical creature from scratch. We'll give it a body, a heart, reflexes, missions, and even a memory.
Phase 1: The Body. Before it can think, it must exist.
The Blueprint
We need a class definition.
- Class Name:
Golem - Attributes:
name: What we call it.energy: Its battery level. It should start fully charged at100.
class Robot:
def __init__(self, name):
self.name = name
self.battery = 100 # Default value
1
Define the Class
Create a class named Golem.
2
Initialize it
Write the __init__ method. It should take self and name as arguments.
3
Set Attributes
Inside __init__:
- Set
self.nameto thenameargument. - Set
self.energyto100(Every Golem starts fresh!).
Remember indentation! The def __init__ must be inside the class, and the assignments must be inside the def.
Suggested SolutionExpandCollapse
ExpandCollapse
Solution:
class Golem:
def __init__(self, name):
self.name = name
self.energy = 100
# Testing the Golem
bob = Golem("Bob")
print(bob.name)
print(bob.energy)Loading...
Terminal
Terminal
Ready to run...