├── 1.py ├── 2.py ├── 3.py └── 4.py /1.py: -------------------------------------------------------------------------------- 1 | class Example: 2 | static_var1 = 10 3 | static_var2 = 20 4 | 5 | def __init__(self, dynamic_var1, dynamic_var2): 6 | self.dynamic_var1 = dynamic_var1 7 | self.dynamic_var2 = dynamic_var2 8 | 9 | def method1(self): 10 | self.new_var = 5 11 | print(f"Метод 1: {self.new_var}") 12 | 13 | @classmethod 14 | def method2(cls): 15 | return Example.static_var1 + Example.static_var2 16 | 17 | def method3(self): 18 | return self.dynamic_var1 ** self.dynamic_var2 19 | 20 | 21 | x = int(input("Введите переменную x: ")) 22 | y = int(input("Введите переменную y: ")) 23 | 24 | obj = Example(x, y) 25 | 26 | obj.method1() 27 | print(f"Метод 2: {Example.method2()}") 28 | print(f"Метод 3: {obj.method3()}") 29 | -------------------------------------------------------------------------------- /2.py: -------------------------------------------------------------------------------- 1 | class Door: 2 | def __init__(self, width, height): 3 | self.width = width 4 | self.height = height 5 | 6 | def square(self): 7 | return self.width * self.height 8 | 9 | 10 | class Window: 11 | def __init__(self, width, height): 12 | self.width = width 13 | self.height = height 14 | 15 | def square(self): 16 | return self.width * self.height 17 | 18 | 19 | class Room: 20 | def __init__(self): 21 | while True: 22 | self.doors = [] 23 | self.windows = [] 24 | 25 | self.length = float(input("Введите длину комнаты: ")) 26 | self.width = float(input("Введите ширину комнаты: ")) 27 | self.height = float(input("Введите высоту комнаты: ")) 28 | 29 | if self.length <= 0 or self.width <= 0 or self.height <= 0: 30 | print("Длина, ширина и высота комнаты должны быть положительными значениями") 31 | else: 32 | break 33 | 34 | self.square = 2 * self.height * (self.length + self.width) 35 | 36 | while True: 37 | self.num_doors = int(input("Введите количество дверей: ")) 38 | if self.num_doors < 1: 39 | print("Количество дверей не может быть меньше 1") 40 | else: 41 | break 42 | 43 | for i in range(self.num_doors): 44 | while True: 45 | width = float(input(f"Введите ширину двери {i + 1}: ")) 46 | height = float(input(f"Введите высоту двери {i + 1}: ")) 47 | if width <= 0 or height <= 0: 48 | print("Ширина и высота двери должны быть положительными значениями") 49 | else: 50 | self.doors.append(Door(width, height)) 51 | break 52 | 53 | while True: 54 | self.num_windows = int(input("Введите количество окон: ")) 55 | if self.num_windows < 1: 56 | print("Количество дверей не может быть меньше 1") 57 | else: 58 | break 59 | 60 | for i in range(self.num_windows): 61 | while True: 62 | width = float(input(f"Введите ширину окна {i + 1}: ")) 63 | height = float(input(f"Введите высоту окна {i + 1}: ")) 64 | if width <= 0 or height <= 0: 65 | print("Ширина и высота окна должны быть положительными значениями") 66 | else: 67 | self.windows.append(Window(width, height)) 68 | break 69 | 70 | def calculate(self): 71 | square = self.square 72 | for i in range(self.num_doors): 73 | square -= self.doors[i].square() 74 | for i in range(self.num_windows): 75 | square -= self.windows[i].square() 76 | return square 77 | 78 | 79 | room = Room() 80 | print(f"Общая площадь комнаты: {room.square}") 81 | print(f"Площадь после вычета окон и дверей: {room.calculate()}") 82 | -------------------------------------------------------------------------------- /3.py: -------------------------------------------------------------------------------- 1 | class Car: 2 | def __init__(self): 3 | self.speed = 0 4 | self.color = "" 5 | self.name = "" 6 | self.is_police = False 7 | 8 | def go(self): 9 | print(f"{self.name} поехала") 10 | 11 | def stop(self): 12 | print(f"\n{self.name} остановилась\n") 13 | 14 | def turn(self, direction): 15 | print(f"{self.name} повернула {direction}") 16 | 17 | def show_speed(self): 18 | print(f"\nТекущая скорость {self.name} - {self.speed} км/ч\n") 19 | 20 | def info(self): 21 | print(f"{self.name}") 22 | print(f"Цвет: {self.color}") 23 | print(f"Начальная скорость: {self.speed}\n") 24 | 25 | 26 | class TownCar(Car): 27 | def __init__(self): 28 | super().__init__() 29 | self.speed = 60 30 | self.color = "черный" 31 | self.name = "Городская машина" 32 | 33 | def show_speed(self): 34 | super().show_speed() 35 | if self.speed > 80: 36 | print("! Превышение скорости !\n") 37 | 38 | 39 | class SportCar(Car): 40 | def __init__(self): 41 | super().__init__() 42 | self.speed = 100 43 | self.color = "красный" 44 | self.name = "Спортивная машина" 45 | 46 | def show_speed(self): 47 | super().show_speed() 48 | if self.speed > 80: 49 | print("! Превышение скорости !\n") 50 | 51 | 52 | class WorkCar(Car): 53 | def __init__(self): 54 | super().__init__() 55 | self.speed = 50 56 | self.color = "желтый" 57 | self.name = "Рабочая машина" 58 | 59 | def show_speed(self): 60 | super().show_speed() 61 | if self.speed > 60: 62 | print("! Превышение скорости !\n") 63 | 64 | 65 | class PoliceCar(Car): 66 | def __init__(self): 67 | super().__init__() 68 | self.speed = 90 69 | self.color = "синий" 70 | self.name = "Милицейская машина" 71 | self.is_police = True 72 | 73 | 74 | def choice_car(cars): 75 | if not cars: 76 | print("Машин нет!") 77 | 78 | while True: 79 | print("\nВыберите машину для начала поездки:\n") 80 | 81 | for i in range(len(cars)): 82 | print("-------------") 83 | print(f"| Машина №{i + 1} |") 84 | print("-------------") 85 | cars[i].info() 86 | 87 | try: 88 | choice = int(input("Ваш выбор: ")) 89 | if 1 <= choice <= len(cars): 90 | return choice 91 | else: 92 | print("Введите верное значение!") 93 | except ValueError: 94 | print("Введите верное значение!") 95 | 96 | 97 | def choice_action(): 98 | print("1. Прибавить скорость") 99 | print("2. Убавить скорость") 100 | print("3. Повернуть") 101 | print("4. Сменить машину") 102 | try: 103 | choice = int(input("Ваш выбор: ")) 104 | if 1 <= choice <= 4: 105 | return choice 106 | else: 107 | print("Введите верное значение!") 108 | except ValueError: 109 | print("Введите верное значение!") 110 | 111 | 112 | cars = [] 113 | 114 | town_car = TownCar() 115 | cars.append(town_car) 116 | 117 | sport_car = SportCar() 118 | cars.append(sport_car) 119 | 120 | work_car = WorkCar() 121 | cars.append(work_car) 122 | 123 | police_car = PoliceCar() 124 | cars.append(police_car) 125 | 126 | while True: 127 | index_choice_car = choice_car(cars) 128 | current_car = cars[index_choice_car - 1] 129 | 130 | print(f"Вы выбрали '{current_car.name}'!\n") 131 | current_car.go() 132 | current_car.show_speed() 133 | 134 | while True: 135 | choice = choice_action() 136 | 137 | if choice == 1: 138 | current_car.speed += 10 139 | current_car.show_speed() 140 | elif choice == 2: 141 | if current_car.speed == 10: 142 | current_car.stop() 143 | else: 144 | current_car.speed -= 10 145 | current_car.show_speed() 146 | elif choice == 3: 147 | direction = input("\nВведите направление: ") 148 | current_car.turn(direction) 149 | current_car.show_speed() 150 | else: 151 | current_car.stop() 152 | break 153 | -------------------------------------------------------------------------------- /4.py: -------------------------------------------------------------------------------- 1 | class Person: 2 | # Методы экземпляра 3 | def __init__(self, surname, name, age, height): 4 | self.surname = surname 5 | self.name = name 6 | self.age = age 7 | self.height = height 8 | 9 | def get_age(self): 10 | return self.age 11 | 12 | def print_info(self): 13 | print(f"\nФамилия: {self.surname}") 14 | print(f"Имя: {self.name}") 15 | print(f"Возраст: {self.age}") 16 | print(f"Рост: {self.height}") 17 | 18 | # Статические методы 19 | 20 | @staticmethod 21 | def is_adult(age): 22 | return age >= 18 23 | 24 | @staticmethod 25 | def correct_age(age): 26 | try: 27 | if 1 <= age <= 120: 28 | return True 29 | else: 30 | return False 31 | except ValueError: 32 | return False 33 | 34 | @staticmethod 35 | def correct_height(age): 36 | try: 37 | if 50 <= age <= 240: 38 | return True 39 | else: 40 | return False 41 | except ValueError: 42 | return False 43 | 44 | # Метод класса 45 | 46 | @classmethod 47 | def from_string(cls, info_str): 48 | last_name, first_name, age, height = info_str.split() 49 | return cls(last_name, first_name, age, height) 50 | 51 | 52 | person1 = Person("Андреенко", "Павел", 18, 175) # Метод экземпляра 53 | person1.print_info() # Метод экземпляра 54 | 55 | if Person.is_adult(person1.get_age()): # Статический метод 56 | print("Совершеннолетний(ая)") 57 | else: 58 | print("Несовершеннолетний(ая)") 59 | 60 | while True: 61 | info_str = input("\nВведите данные о человеке через пробел в формате 'ФАМИЛИЯ ИМЯ ВОЗРАСТ РОСТ': ") 62 | 63 | info = info_str.split() 64 | 65 | if len(info) == 4: 66 | if not Person.correct_age(int(info[2])): # Статический метод 67 | print("Введите корректное значение возраста!") 68 | elif not Person.correct_height(int(info[3])): # Статический метод 69 | print("Введите корректное значение роста!") 70 | else: 71 | person2 = Person.from_string(info_str) # Метод класса 72 | person2.print_info() # Метод экземпляра 73 | else: 74 | print("Данные введены неверно!") 75 | --------------------------------------------------------------------------------