Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """дз от 06.11.2024г."""
- class House:
- def __init__(self, name, number_of_floors):
- self.name = name
- self.number_of_floors = number_of_floors
- def go_to(self, new_floor):
- if new_floor not in range(1, self.number_of_floors + 1):
- print('Такого этажа не существует\n')
- if new_floor in range(1, self.number_of_floors + 1):
- for floor in range(1, new_floor + 1):
- print(floor)
- def __len__(self):
- return self.number_of_floors
- def __str__(self):
- return f'Название: {self.name}, кол-во этажей: {self.number_of_floors}'
- def __eq__(self, other):
- if isinstance(other, House):
- return self.number_of_floors == other.number_of_floors
- return False
- def __lt__(self, other):
- if isinstance(other, House):
- return self.number_of_floors < other.number_of_floors
- return False
- def __le__(self, other):
- if isinstance(other, House):
- return self.number_of_floors <= other.number_of_floors
- return False
- def __gt__(self, other):
- if isinstance(other, House):
- return self.number_of_floors > other.number_of_floors
- return False
- def __ge__(self, other):
- if isinstance(other, House):
- return self.number_of_floors >= other.number_of_floors
- return False
- def __ne__(self, other):
- if isinstance(other, House):
- return self.number_of_floors != other.number_of_floors
- return False
- def __add__(self, value):
- if isinstance(value, int):
- self.number_of_floors += value
- return self
- def __radd__(self, value):
- if isinstance(value, int):
- return self.__add__(value)
- return self
- def __iadd__(self, value):
- if isinstance(value, int):
- self.number_of_floors += value
- return self
- """Пример использования"""
- h1 = House('ЖК Эльбрус', 10)
- h2 = House('ЖК Акация', 20)
- print(h1)
- print(h2)
- print(h1 == h2) # __eq__
- h1 = h1 + 10 # __add__
- print(h1)
- print(h1 == h2)
- h1 += 10 # __iadd__
- print(h1)
- h2 = 10 + h2 # __radd__
- print(h2)
- print(h1 > h2) # __gt__
- print(h1 >= h2) # __ge__
- print(h1 < h2) # __lt__
- print(h1 <= h2) # __le__
- print(h1 != h2) # __ne__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement