Learn Interfaces by Building an Equation Solver - Step 15

Tell us what’s happening:

can’t seem to get it right. my code is the following:

 if any(isinstance(arg, (int, float)) for arg in args) == False:
        raise TypeError("Coefficients must be of type 'int' or 'float'")

Your code so far

from abc import ABC, abstractmethod


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

# User Editable Region

        if any(isinstance(arg, (int, float)) for arg in args) == False:
            raise TypeError("Coefficients must be of type 'int' or 'float'")

# User Editable Region

    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)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15

Challenge Information:

Learn Interfaces by Building an Equation Solver - Step 15

1 Like

There’s an issue with the logic. Let’s say that you have an int and a string. any gives you True, but no error is raised.

That would have worked with all(), though.

My suggestion is to modify the expression of the generator expression and avoid to compare the result of any with anything.

1 Like

Hi,

Thanks for your reply. I did this and it worked:

if any(not isinstance(arg, (int, float)) for arg in args):
        raise TypeError("Coefficients must be of type 'int' or 'float'")

Yeah, but what’s the point of removing the for loop, if afterwards we say for arg in args?

" Step 15

Now, replace the for loop and if statement you added in the previous step with an if statement that uses the any() built-in function."

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.