Learn Interfaces by Building an Equation Solver - Step 17

Tell us what’s happening:

what do i do expression wise …

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"'Equation' object takes {self.degree + 1} positional arguments but {len(args)} were given"
            )
        if any(not isinstance(arg, (int, float)) for arg in args):
            raise TypeError("Coefficients must be of type 'int' or 'float'")
        if args[0] == 0:
            raise ValueError("Highest degree coefficient must be different from zero")

# User Editable Region

    coefficients = {key : (expression) for (key,value) in args}    

# 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/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Interfaces by Building an Equation Solver - Step 17

also ive edited my values to look like this

coefficients = {0 : (expression) for (0,args) in arg

First of all, check the indentation. Currently, you are outside the __init__ method.

Second, think about what you need to do to create an instance attribute.

Then, we can talk about the dictionary comprehension.

self.coefficients = {0 : (expression) for (0,args) in args}
is this what you mean and if yes why? whats the point of the self? why can’t i do it without

If you ever want to be a self sufficient programmer, you need get used to doing a little bit of research on your own to answer all of these questions:

https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-self-parameter-why-is-it-needed

https://www.geeksforgeeks.org/self-in-python-class/

1 Like

is chatgpt useful for this i mean i use it for research sometimes but limit it as i feel like i am cheating myself

What is wrong with the explanations on the sites above?

A lot of debate on this recently, YMMV, I think it depends on how you use it.

You can ask it what this function does or what this code does and try to explain to you. Don’t use it to write code bc that is what you are trying to learn to do…

Also keep in mind that it’s not a source of truth. It’s not designed as a programming teacher, it’s jut a probabilistic sentence generator.

Most concepts and functions are already explained well in great detail on the Internet, AI is of limited dubious use.

You can tell when ML creates an image with too many fingers but if you are learning to code you can’t tell if it generates code with too many fingers.

1 Like

ti’m not a fan of sites, because i can’t ask question and get a response instantly like that sel site you sent me i have read it and it’s telling me the self parameter is used because you get to customise an object when calling it, but i still don’t understand how it works, i don;t get how it applies to my code.

A class is a blueprint for a building. In the building there will be a boardroom. Since this is just a blueprint, and you don’t know what someone would call a building they make for it, you refer to the boardroom as self.boardroom

Later, someone uses the blueprint to make a building called Skytower. Well when you want to refer to the boardroom in that building it’s called Skytower.boardroom.

Much later someone makes a building using the same blueprint called Skypiercer. When you refer to that boardroom it’s called Skypiercer.boardroom and it won’t get mixed up with Skytower.boardroom, a boardroom in a different building made with the same blueprint.

okay that’s helpful my code looks like this now

self.coefficents = {0 : {1: 2,0: 3} for key,val1,val2 in args}  
        print(coefficents.degree)

Try to explain what you are trying to achieve here.
I can assume what you are trying to do but this is not how dictionary comprehensions work.

You cannot try to hardcode the values, otherwise there’s no point in writing a comprehension. You need to dynamically compute the keys and values of your new dictionary.

Your comprehension should have the following structure:

{key: val for i, j in iterable}

Where key: val is the key-value pair (to compute dynamically) and i, j are the iteration variables. To be able to use two variables, you need to keep track of both the items in args and their index (there’s is a specific function for that in Python).

So, iterable would not be simply args.

Also, you need to find a way to create the keys so that their value decreases while the iteration proceed.

well the thing im trying to do when i write 0 : {1: 2,0: 3} is the key 0 and the value {1: 2,0: 3} bascially the dgree being 0 and the value being {1: 2,0: 3}

so i’ve now got self.coefficents = {0 : 0: 2,0: 3 for val1,val2 in enumerate(args)}
ill fix the key and value bit just now but if u see it before i fix it is this right?

is this what you mean? {self.degree : 2,3 for val1,val2 in enumerate(args)}

self.coefficents = {self.degree : {0: 2, 1: 3} for val1,val2 in enumerate(args)}  

im so fucking close its giving me this error You should declare an attribute named coefficients within your __init__ method.

There’s not really a good reason to curse here.

Obvious question but is this line in your __init__ method?

Yeah sorry about the cursing part lol, working on this for about almost a day now, I’m sure you know how it feels when you know you are becoming closer to the finish line after 2 hours of work on it .

from abc import ABC, abstractmethod

class Equation(ABC):
    degree: int
    
    def __init__(self, *args):
        if (self.degree + 1) != len(args):
            raise TypeError(
                f"'Equation' object takes {self.degree + 1} positional arguments but {len(args)} were given"
            )
        if any(not isinstance(arg, (int, float)) for arg in args):
            raise TypeError("Coefficients must be of type 'int' or 'float'")
        if args[0] == 0:
            raise ValueError("Highest degree coefficient must be different from zero")
        self.coefficents = {self.degree : {0: 2, 1: 3} for val1,val2 in enumerate(args)}  
        
    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) 

this is in my __init__method no??

It is now, it wasn’t in your first post.

What if you create an equation with 3 or 4 coefficients?

Refer to the rest of @Dario_DC 's comment.

Review and understand how to do a dictionary comprehension.