From d6bf1b7426b44f6ad967fa1d6745e3fa7688a254 Mon Sep 17 00:00:00 2001 From: Anastasia Ivleva <13natia13@gmail.com> Date: Wed, 26 Mar 2025 17:42:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20=D0=A4=D0=B8?= =?UTF-8?q?=D1=82=D0=BD=D0=B5=D1=81-=D1=82=D1=80=D0=B5=D0=BA=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + homework.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 13f592dc..216ea7be 100644 --- a/.gitignore +++ b/.gitignore @@ -104,6 +104,7 @@ celerybeat.pid # Environments .env .venv +.venv/ env/ venv/ ENV/ diff --git a/homework.py b/homework.py index a1966cf0..735170b3 100644 --- a/homework.py +++ b/homework.py @@ -1,25 +1,52 @@ +from pyexpat.errors import messages + + class InfoMessage: """Информационное сообщение о тренировке.""" - pass + def __init__(self, + training_type: str, + duration: float, + distance: float, + speed: float, + calories: float) -> None: + self.training_type = training_type + self.duration = duration + self.distance = distance + self.speed = speed + self.calories = calories + + def get_message(self) -> str: + message_result = (f'Тип тренировки: {self.training_type}; ' + f'Длительность: {self.duration:.3f} ч.; ' + f'Дистанция: {self.distance:.3f} км; ' + f'Ср. скорость: {self.speed:.3f} км/ч; ' + f'Потрачено ккал: {self.calories:.3f}.') + return message_result class Training: """Базовый класс тренировки.""" + LEN_STEP = 0.65 + M_IN_KM = 1000 + MIN_IN_H = 60 + def __init__(self, action: int, duration: float, weight: float, ) -> None: - pass + self.action = action + self.duration = duration + self.weight = weight def get_distance(self) -> float: """Получить дистанцию в км.""" - pass + return self.action * self.LEN_STEP / self.M_IN_KM def get_mean_speed(self) -> float: """Получить среднюю скорость движения.""" - pass + return self.get_distance() / self.duration def get_spent_calories(self) -> float: """Получить количество затраченных калорий.""" @@ -27,32 +54,95 @@ def get_spent_calories(self) -> float: def show_training_info(self) -> InfoMessage: """Вернуть информационное сообщение о выполненной тренировке.""" - pass + info = InfoMessage( + training_type=self.__class__.__name__, + duration=self.duration, + distance=self.get_distance(), + speed=self.get_mean_speed(), + calories=self.get_spent_calories() + ) + return info class Running(Training): """Тренировка: бег.""" - pass + + CALORIES_MEAN_SPEED_MULTIPLIER = 18 + CALORIES_MEAN_SPEED_SHIFT = 1.79 + + def get_spent_calories(self) -> float: + """Получить количество затраченных калорий при беге.""" + return ((self.CALORIES_MEAN_SPEED_MULTIPLIER * self.get_mean_speed() + + self.CALORIES_MEAN_SPEED_SHIFT) + * self.weight / self.M_IN_KM * self.duration * self.MIN_IN_H) class SportsWalking(Training): """Тренировка: спортивная ходьба.""" - pass + + CALORIES_WEIGHT_COEFFICIENT = 0.035 + CALORIES_WEIGHT_SPEED_COEFFICIENT = 0.029 + SM_IN_M = 100 + KM_H_IN_M_S = 0.278 + + def __init__(self, + action: int, + duration: float, + weight: float, + height: float + ) -> None: + super().__init__(action, duration, weight) + self.height = height + + def get_spent_calories(self) -> float: + """Получить количество затраченных калорий при спортивной ходьбе.""" + return ((self.CALORIES_WEIGHT_COEFFICIENT * self.weight + + ((self.get_mean_speed() * self.KM_H_IN_M_S) ** 2 + / (self.height / self.SM_IN_M)) + * self.CALORIES_WEIGHT_SPEED_COEFFICIENT + * self.weight) * self.duration * self.MIN_IN_H) class Swimming(Training): """Тренировка: плавание.""" - pass + + LEN_STEP = 1.38 + MEAN_SPEED_MULTIPLIER = 1.1 + CALORIES_SPEED_MULTIPLIER = 2 + + def __init__(self, + action: int, + duration: float, + weight: float, + length_pool, + count_pool + ) -> None: + super().__init__(action, duration, weight) + self.length_pool = length_pool + self.count_pool = count_pool + + def get_mean_speed(self) -> float: + return (self.length_pool * self.count_pool + / self.M_IN_KM / self.duration) + + def get_spent_calories(self) -> float: + return ((self.get_mean_speed() + self.MEAN_SPEED_MULTIPLIER) + * self.CALORIES_SPEED_MULTIPLIER * self.weight * self.duration) def read_package(workout_type: str, data: list) -> Training: """Прочитать данные полученные от датчиков.""" - pass + workout_type_dict = {'SWM': Swimming, + 'RUN': Running, + 'WLK': SportsWalking} + result = workout_type_dict[workout_type](*data) + return result def main(training: Training) -> None: """Главная функция.""" - pass + info = training.show_training_info() + print(info.get_message()) if __name__ == '__main__': @@ -65,4 +155,3 @@ def main(training: Training) -> None: for workout_type, data in packages: training = read_package(workout_type, data) main(training) -