Untitled
unknown
plain_text
2 years ago
5.3 kB
1
Indexable
LEN_STEP = 0.65 LEN_PADDLE = 1.38 M_IN_KM = 1000 CALORIES_MEAN_SPEED_MULTIPLIER = 18 CALORIES_MEAN_SPEED_SHIFT = 1.79 class InfoMessage: """Информационное сообщение о тренировке.""" def __init__(self, training_type, duration, distance, speed, calories): self.training_type = training_type self.duration = duration self.distance = distance self.speed = speed self.calories = calories def get_message(self): print(f'Тип тренировки: {self.training_type}' f'Длительность: {self.duration} ч.;' f'Дистанция: {self.distance} км; ' f'Ср. скорость: {self.speed} км/ч; ' f'Потрачено ккал: {self.calories}.') class Training: """Базовый класс тренировки.""" def __init__(self, action: int, duration: float, weight: float, ) -> None: self.action = action self.duration = duration self.weight = weight def get_distance(self) -> float: """Получить дистанцию в км.""" dist_in_km = self.action * LEN_STEP / M_IN_KM return dist_in_km def get_mean_speed(self) -> float: """Получить среднюю скорость движения.""" mean_speed = self.get_distance() / self.duration return mean_speed def get_spent_calories(self) -> float: """Получить количество затраченных калорий.""" spent_calories = (K_1 * self.weight + (self.get_mean_speed() ** 2 / HEIGHT) * K_2 * self.weight) * self.duration return spent_calories def show_training_info(self) -> InfoMessage: """Вернуть информационное сообщение о выполненной тренировке.""" training_type = self.__class__.__name__ duration = round(self.duration, 2) distance = round(self.get_distance(), 2) speed = round(self.get_mean_speed(), 2) calories = round(self.get_spent_calories(), 2) return InfoMessage(training_type, duration, distance, speed, calories) class Running(Training): """Тренировка: бег.""" def __init__(self, action: int, duration: float, weight: float): super().__init__(action, duration, weight) self.action = action self.duration = duration self.weight = weight def get_spent_calories(self) -> float: total_calories = (((CALORIES_MEAN_SPEED_MULTIPLIER * self.get_mean_speed() + CALORIES_MEAN_SPEED_SHIFT) * self.weight / M_IN_KM * (self.duration * 60))) return total_calories class SportsWalking(Training): """Тренировка: спортивная ходьба.""" def __init__(self, action: int, duration: float, weight: float, height): super().__init__(action, duration, weight) self.height = height def get_spent_calories(self) -> float: total_calories = ((0.035 * self.weight + ((self.get_mean_speed() / 1000 * 3600) ** 2 / (self.height / 100) * 0.029 * self.weight) * (self.duration * 60))) return total_calories class Swimming(Training): """Тренировка: плавание.""" def __init__(self, action: int, duration: float, weight: float, length_pool, count_pool): self.legth_pool = length_pool self.count_pool = count_pool super().__init__(action, duration, weight) def get_mean_speed(self) -> float: """Переопределение метода средней скорости для плавания.""" mean_speed_swimming = self.legth_pool * self.count_pool / M_IN_KM / self.duration return mean_speed_swimming def get_spent_calories(self) -> float: """Переопределение расчёта израсходованных калорий.""" total_calories = (self.get_mean_speed() + 1.1) * 2 * self.weight * self.duration return total_calories def read_package(workout_type: str, data: list) -> Training: """Прочитать данные полученные от датчиков.""" trainings = {'SWM': Swimming, 'RUN': Running, 'WLK': SportsWalking} get_training_class = trainings.get(workout_type) training_object = get_training_class(*data) return training_object def main(training: Training) -> None: """Главная функция.""" info = training.show_training_info() message = info.get_message() print(message) if __name__ == '__main__': packages = [ ('SWM', [720, 1, 80, 25, 40]), ('RUN', [15000, 1, 75]), ('WLK', [9000, 1, 75, 180]), ] for workout_type, data in packages: training = read_package(workout_type, data) main(training)
Editor is loading...