Learn Interfaces by Building an Equation Solver - Step 17

Tell us what’s happening:

I’m pretty lost with this one. Tried so many different things. Am I at least close? Thanks.

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:

# User Editable Region

            raise ValueError("Highest degree coefficient must be different from zero")

        self.coefficients = dict({degree : coeff for degree, coeff in reversed(tuple(enumerate(reversed(args))))})
        print(self.coefficients)




# 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Learn Interfaces by Building an Equation Solver - Step 17

Very close, but look at the output here:

args = (2,4)
{arg:idx for idx,arg in enumerate(args)}
{2: 0, 4: 1}

The output you want is:

{1: 2, 0: 4}

I still don’t get it, the code I have is returning the right output the {1: 2, 0: 3} with the print statement…

My mistake, I didn’t copy your code above :grimacing: Sorry about that!

You do get a correct result but the code is a bit more complex than it needs to be (two reverses, etc.)

Looking at your answer again:

dict({degree : coeff for degree, coeff in reversed(tuple(enumerate(reversed(args))))})

You have some unnecessary things. Get rid of all the extra stuff:

{degree : coeff for degree, coeff in enumerate(args)}
{0: 5, 1: 4}

Only change this:

{degree ...}

Leave the rest as it is. Can you modify the first element here to give the correct output?

{1: 5, 0: 4}

This is what you need to do to pass the test.

I’m still really lost. Just do something different besides degree?

Oh nevermind I got it. I read through the forum posts and that helped me! Thank you! You also helped!

1 Like

You’re extremely close to the answer.

Imagine you have this list, degree = [0,1,2,3]

You have a loop like this:

degree = [0,1,2,3]

for n in range(len(degree)):
    print(n)

>>> 0
>>> 1
>>> 2
>>> 3

It doesn’t even matter what the contents of degree are, it could be degree = ["cars","boats","planes","scooters"] and the output would still just be 0,1,2,3.

How would you change print(n) to print 3,2,1,0 ?