curriculum/challenges/english/blocks/learn-interfaces-by-building-an-equation-solver/6675a38a8b535e4ff3274520.md
The hasattr built-in function takes an object as its first argument and a string representing an attribute name as its second argument. It returns a boolean indicating if the object has the specified attribute.
Now you are going to use the __init_subclass__ method to check if the child class has the degree attribute at the moment of the instantiation.
Create an if statement to check if cls does not have a degree attribute. If so, raise an AttributeError and use the string f"Cannot create '{cls.__name__}' class: missing required attribute 'degree'" to provide a custom message.
After that, fix the error that has appeared in the terminal by declaring a degree class attribute inside the LinearEquation class. This attribute should represent the degree of the equation, which is the exponent of the highest \( x \) term. Therefore, assign the integer 1 to the degree attribute.
You should create an if statement that checks if cls does not have the attribute degree inside the __init_subclass__ method.
({ test: () => assert(runPython(`_Node(_code).find_class("Equation").find_function("__init_subclass__").find_ifs()[0].find_conditions()[0].is_equivalent("not hasattr(cls, 'degree')")`)) })
You should raise an AttributeError using the provided string inside your if statement.
({ test: () => runPython(`
raise_stmt = 'raise AttributeError(f"Cannot create \\'{cls.__name__}\\' class: missing required attribute \\'degree\\'")'
node = _Node(_code).find_class("Equation").find_function("__init_subclass__").find_ifs()[0].find_bodies()[0]
assert node.has_stmt(raise_stmt)
`) })
from abc import ABC, abstractmethod
class Equation(ABC):
degree: int
def __init__(self):
pass
--fcc-editable-region--
def __init_subclass__(cls):
pass
@abstractmethod
def solve(self):
pass
@abstractmethod
def analyze(self):
pass
class LinearEquation(Equation):
--fcc-editable-region--
def solve(self):
pass
def analyze(self):
pass
lin_eq = LinearEquation()