Advertisement
JmihPodvalbniy

Untitled

Nov 11th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | Software | 0 0
  1. """дз от 06.11.2024г."""
  2.  
  3. class House:
  4.     def __init__(self, name, number_of_floors):
  5.         self.name = name
  6.         self.number_of_floors = number_of_floors
  7.  
  8.     def go_to(self, new_floor):
  9.         if new_floor not in range(1, self.number_of_floors + 1):
  10.             print('Такого этажа не существует\n')
  11.         if new_floor in range(1, self.number_of_floors + 1):
  12.             for floor in range(1, new_floor + 1):
  13.                 print(floor)
  14.  
  15.     def __len__(self):
  16.         return self.number_of_floors
  17.  
  18.     def __str__(self):
  19.         return f'Название: {self.name}, кол-во этажей: {self.number_of_floors}'
  20.  
  21.     def __eq__(self, other):
  22.         if isinstance(other, House):
  23.             return self.number_of_floors == other.number_of_floors
  24.         return False
  25.  
  26.     def __lt__(self, other):
  27.         if isinstance(other, House):
  28.             return self.number_of_floors < other.number_of_floors
  29.         return False
  30.  
  31.     def __le__(self, other):
  32.         if isinstance(other, House):
  33.             return self.number_of_floors <= other.number_of_floors
  34.         return False
  35.  
  36.     def __gt__(self, other):
  37.         if isinstance(other, House):
  38.             return self.number_of_floors > other.number_of_floors
  39.         return False
  40.  
  41.     def __ge__(self, other):
  42.         if isinstance(other, House):
  43.             return self.number_of_floors >= other.number_of_floors
  44.         return False
  45.  
  46.     def __ne__(self, other):
  47.         if isinstance(other, House):
  48.             return self.number_of_floors != other.number_of_floors
  49.         return False
  50.  
  51.     def __add__(self, value):
  52.         if isinstance(value, int):
  53.             self.number_of_floors += value
  54.         return self
  55.  
  56.     def __radd__(self, value):
  57.         if isinstance(value, int):
  58.             return self.__add__(value)
  59.         return self
  60.  
  61.     def __iadd__(self, value):
  62.         if isinstance(value, int):
  63.             self.number_of_floors += value
  64.         return self
  65.  
  66. """Пример использования"""
  67. h1 = House('ЖК Эльбрус', 10)
  68. h2 = House('ЖК Акация', 20)
  69. print(h1)
  70. print(h2)
  71. print(h1 == h2) # __eq__
  72. h1 = h1 + 10 # __add__
  73. print(h1)
  74. print(h1 == h2)
  75. h1 += 10 # __iadd__
  76. print(h1)
  77. h2 = 10 + h2 # __radd__
  78. print(h2)
  79. print(h1 > h2) # __gt__
  80. print(h1 >= h2) # __ge__
  81. print(h1 < h2) # __lt__
  82. print(h1 <= h2) # __le__
  83. print(h1 != h2) # __ne__
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement