-3

I'm trying to write a code and write test after this. But i have a problem. Only 1 functions gives the output. I didn't even get into the others. What is the reason? Why my programm ends before even 'visiting all the functions'. In my code I can't get into degree and coefficient. What is wrong with this code.

class Polynomia1:
    def __init__(self, data=[]):
        if len(data) > 0:
            data = sorted(data, key=lambda x: x[0])
            data.reverse()
            b= []
            for elem in data:
                if elem[0] < 0:
                    raise ValueError("Negative power is not allowed")
                if elem[0] >= 0:
                    b.append(elem[0])
                if elem[1] == 0:
                    raise ValueError("coefficient cannot be empty")
            if len(data) > len(set(b)):
                raise ValueError("The value of power cannot be repeated")
        self.data = data
    def create_equation(self):
        a = ""
        if len(self.data) == 0:
            a+= '0'
        else:
            for num in self.data:
                if num[0] != 0 and num[0] != 1 and num[1] != 1 and num[1] != -1:
                    if len(a) == 0 or num[1] < 0:
                        a += f'{num[1]}x^{num[0]}'
                    else:
                        a += f'+{num[1]}x^{num[0]}'
                if num[1] == -1 and num [0] != 0 and num[0] != 1:
                    a += f'-x^{num[0]}'
                if num[1] == 1 and num [0] != 0 and num[0] != 1 and len(a) > 0:
                    a += f'+x^{num[0]}'
                if num[1] == 1 and num [0] != 0 and num[0] != 1 and len(a) == 0:
                    a += f'x^{num[0]}'              
                if (len(a) == 0 or num[1] <=0)and num[0] == 0:
                    a += str(num[1])
                if len(a) > 0 and num[1] > 0 and num[0] == 0:
                    a += f'+{num[1]}'
                if ((len(a) == 0 or num[1] <=0) and num[1] != -1 and num[1]!=1) and num[0] == 1:
                    a += f'{num[1]}x'
                if ((len(a) == 0 or num[1] <=0) and num[1] == -1 and num[1]!=1) and num[0] == 1:
                    a += f'-x'
                if num[1] > 0 and num[1] != 1 and num [0] == 1 and len(a)>0:
                    a += f'+{num[1]}x'
                if num[1] > 0 and (num[1] == 1 or num[1] == -1) and num [0] == 1 and len(a)>0:
                    a += f'+x'
                if num[1] > 0 and (num[1] == 1 or num[1] == -1) and num [0] == 1 and len(a)==0:
                    a += f'x'
        return a
    def __degree__(self):
        return max(self.data, key=lambda x:x[0])
    def __str__(self):
        """Return information about polynomial"""
        return self.create_equation()
    def coefficient(self,number):
        the_outcome = 0
        for nuum in self.data:
            if nuum[0] == number:
                the_outcome = nuum[1]
            return the_outcome

print(Polynomia1([(3, 5)]))
Kokin
  • 11
  • 6

1 Answers1

2

I think you are asking is why does __str__ get called automatically. The answer is that because print() calls str() on every non-keyword parameter which in turn calls __str__ on the object. The print() documentation states this explicitly.

Python defines several other match "dunder" methods. For example, you can implement the + operator by adding __add__() to your class. __degree__() is not one of these methods. It looks like something you just made up, so there is no way for Python to know to call it. I strongly suggest that you only use __name__ as a method name when you are defining a method that Python actually knows about. In this case, you should name your method degree() instead of __degree__().

Original answer:

Why my programm ends before even 'visiting all the functions'.

Python won't just magically "visit" every function you write. In order to execute the code in a function you have to call it. For example:

p = Polynomial([(3, 5)])
print(p.coefficient(3))
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I know that. But i didn't print the function _str_ but it gives the output. So why the function __degree__ doesn't work like that? – Kokin Nov 26 '21 at 10:49
  • @Kokin Ok, I think I better understand what you are asking. I edited my questions with more details. In short `__str__()` is a special method that Python knows how to use. `__degree__()` is a method that you made up that Python doesn't know anything about. – Code-Apprentice Nov 27 '21 at 05:16