Leamich

class triangle

Sep 21st, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. from dataclasses import dataclass
  2.  
  3.  
  4. class TriangleNotExistException(Exception):
  5.     def __init__(self):
  6.         super().__init__("Triangle with this sides doesn't exist")
  7.  
  8.  
  9. @dataclass
  10. class Triangle:
  11.     """Класс треугольника"""
  12.     a: int
  13.     b: int
  14.     c: int
  15.  
  16.     def __post_init__(self):
  17.         if self.a + self.b <= self.c:
  18.             raise TriangleNotExistException
  19.         if self.b + self.c <= self.a:
  20.             raise TriangleNotExistException
  21.         if self.a + self.c <= self.b:
  22.             raise TriangleNotExistException
  23.  
  24.     @property
  25.     def ttype(self):
  26.         if self.a == self.b == self.c:
  27.             return "equilateral triangle"
  28.         if self.a == self.b or self.c == self.a or self.b == self.c:
  29.             return "isosceles triangle"
  30.         return "normal triangle"
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     tr1 = Triangle(2, 2, 3)
  35.     print(tr1.ttype)
  36.     tr2 = Triangle(1, 1, 100)
  37.  
Advertisement
Add Comment
Please, Sign In to add comment