object oriented – Quadratic equation class in Python OOP

Hello everyone

In today’s assignment I had to write a function to determine if (and how many) the quadratic function defined by the formula f(x) = ax^2 + bx +c has zero places. I had decorators to write.

Here is my solution:

``````from typing import List, Tuple
from math import sqrt
from datetime import datetime

class ZeroError(Exception):
'''Error when calculating roots, and delta is lower than 0.'''
def __init__(self, message = "Delta must be greater than 0!"):
super().__init__(self.message)

def __init__(self, a: int, b: int, c: int):
self.a = a
self.b = b
self.c = c
self.x = -1

@property
def Roots(self):
a, b, c = self.a, self.b, self.c
d = sqrt(b * b - 4 * a * c)
if d > 0:
x1 = (-b + d) / (2 * a)
x2 = (-b - d) / (2 * a)
return x1, x2
if d == 0:
return -b / 2 * a
if d < 0:
raise ZeroError

@property
def Viete(self):
a, b, c = self.a, self.b, self.c
d = b * b - 4 * a * c
if d > 0:
return "x1 + x2 = {x}".format(x = -b / a), "x1 * x2 = {y}".format(y = c / a)

@property
def Vertex(self):
a, b, c = self.a, self.b, self.c
d = b * b - 4 * a * c
return "W: ({p}, {q})".format(p = -b / 2 * a, q = -d / 4 * a)

@property
def Time_dependent(self):
if datetime.now().hour + 2 in range(8, 16):
return "Roots"
else: return "Break"

@property
def x_plus_3(self):
a, b, c, x = self.a, self.b, self.c, self.x
x += 3
return x * (x * a + b) + c