Learn Interfaces by Building an Equation Solver - Step 13

Tell us what’s happening:

see i know i’m close but this part i’m really struggling to answer been on this for about 35 minutes now playing around and ive came to this conclusion. The hint the system gives me is You should create an if statement that checks if the number of coefficients used to instantiate the equation is different from degree + 1. I have tried to implement it but i dont know how, i’ve looked online on how to check if the number of something in python and then have tried to implement it but it don’t work

Your code so far

from abc import ABC, abstractmethod

class Equation(ABC):
    degree: int

# User Editable Region

    def __init__(self, *args):
        if len(args) == 0:
            raise TypeError(f"'{self.__class__.__name__}' object takes {self.degree + 1} positional arguments but {len(args)} were given")
        

    
    def __init_subclass__(cls):
        if not hasattr(cls, "degree"):
            raise AttributeError(
                f"Cannot create '{cls.__name__}' class: missing required attribute 'degree'"
            )
    
    @abstractmethod
    def solve(self):
        pass
        
    @abstractmethod
    def analyze(self):
        pass
        
class LinearEquation(Equation):
    degree = 1 
    
    def solve(self):
        pass
    
    def analyze(self):
        pass
    
lin_eq = LinearEquation(2,3)

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Interfaces by Building an Equation Solver - Step 13

create an if statement to check if the length of args is different from the number of coefficients the equation should have (degree + 1)

Some questions for you:
How many arguments should a linear equation have?
How many coefficients does a linear equation have?
What is the degree of a linear equation?

Instructions don’t mention anything about checking if there are zero arguments.

i would like to start off by saying, you never fail to give a great explanation, and i appreciate that, however i’m still struggling with the (degree + 1) part, well at least i think so because my code looks like this
`def init(self, *args,):
if len(args) == 1:
raise TypeError(f"‘{self.class.name}’ object takes {self.degree + 1} positional arguments but {len(args)} were given")

def __init_subclass__(cls):
    if not hasattr(cls, "degree"):
        raise AttributeError(
            f"Cannot create '{cls.__name__}' class: missing required attribute 'degree'"
        )

@abstractmethod
def solve(self):
    pass
    
@abstractmethod
def analyze(self):
    pass

class LinearEquation(Equation):
degree = 1

def solve(self):
    pass

def analyze(self):
    pass

lin_eq = LinearEquation(2,3)`

the hint the system give me is you should create an if statement that checks if the number of coefficients used to instantiate the equation is different from degree + 1 .

so its the same hint but i don’t know how to do the hint how can i instantiate the equation and make it different from degree +1

Well degree is a variable, it gets created lower down in the linearequation class. You can see how to use it here:

Read the error again. Your if statement should check exactly what the error is saying.

You didn’t answer my questions:

How many arguments should a linear equation have? 2
How many coefficients does a linear equation have? 2
What is the degree of a linear equation? 1

4x + 5 = 0

This is a linear equation with degree 1. You can see it has 2 arguments:

LinearEquation(4,5)

Degree variable is created here:

class LinearEquation(Equation):
    degree = 1

So, if args is not equal to degree + 1 then the error should be called.

What you have is “If args equals 1” then call the error:

if len(args) == 1:

so you are telling me i should modify my if statement to look like this?

` if not 2 == self.degree +1:’

also i implemented your question into my code, thats why i never answered them. i’m just confused here because are you saying i should create a new if statement saying that code or are you saying i should modify my if statement to say that code??

` if not 2 == self.degree +1:’

Close, but:

  1. Could you make it more simple by using a different comparison? https://www.w3schools.com/python/gloss_python_comparison_operators.asp
  2. Why do you have the number 2 in there? how does that check the number of arguments?

The error tells you all the variables you need and what should be checked.

 object takes {self.degree + 1} positional arguments but {len(args)} were given

If those variables are not equal, then the error is given.

i done it ! thanks sir

1 Like