diff --git a/lesson-6/task1.py b/lesson-6/task1.py new file mode 100644 index 0000000..b3fb4ad --- /dev/null +++ b/lesson-6/task1.py @@ -0,0 +1,23 @@ +import time +from itertools import cycle + +TRAFFIC_LIGHT_COLORS = ( + ('RED', 7), + ('YELLOW', 2), + ('GREEN', 5), +) + + +class TrafficLight: + def __init__(self): + self.__color = None + + def running(self): + for light in cycle(TRAFFIC_LIGHT_COLORS): + self.__color = light[0] + print(f'Color: {self.__color}. Sleep: {light[1]}s') + time.sleep(light[1]) + + +tl = TrafficLight() +tl.running() diff --git a/lesson-6/task2.py b/lesson-6/task2.py new file mode 100644 index 0000000..09aa1c7 --- /dev/null +++ b/lesson-6/task2.py @@ -0,0 +1,11 @@ +class Road: + def __init__(self, length: int, width: int): + self._length = length + self._width = width + + def calc_asphalt_weight(self, weight_pm: int = 25, depth: int = 5): + return self._width * self._length * weight_pm * depth + + +road = Road(20, 5000) +print(road.calc_asphalt_weight()) diff --git a/lesson-6/task3.py b/lesson-6/task3.py new file mode 100644 index 0000000..d3aa67b --- /dev/null +++ b/lesson-6/task3.py @@ -0,0 +1,23 @@ +class Worker: + def __init__(self, name, surname, position, income): + self.name = name + self.surname = surname + self.position = position + self._income = income + + +class Position(Worker): + def get_full_name(self): + return '%s %s' % (self.name, self.surname) + + def get_total_income(self): + try: + return self._income['wage'] + self._income['bonus'] + except TypeError: + print('Income dict error') + return 0 + + +worker_1 = Position('NAME', 'SURNAME', 'POSITION', {"wage": 1234, "bonus": 5678}) +print(worker_1.get_full_name()) +print(worker_1.get_total_income()) diff --git a/lesson-6/task4.py b/lesson-6/task4.py new file mode 100644 index 0000000..a883511 --- /dev/null +++ b/lesson-6/task4.py @@ -0,0 +1,80 @@ +import random + + +class Car: + _speed = 0 + _is_police = False + + def __init__(self, name, color): + self.color = color + self.name = name + + def __str__(self): + return 'Car: %s (%s)' % (self.name, self.color) + + def go(self, speed): + self._speed = speed + self.show_speed() + + def stop(self): + self._speed = 0 + self.show_speed() + + def turn(self, direction): + if self._speed > 0: + print('%s turn %s' % (self, direction)) + else: + print('%s stopped and can`t turn' % self) + + def show_speed(self): + if self._speed == 0: + print('%s. Stopped' % self) + else: + print('%s. Speed %d' % (self, self._speed)) + # return self._speed + + def print_is_police(self): + if self._is_police: + print('%s is police' % self) + else: + print('%s is not police' % self) + + +class TownCar(Car): + def show_speed(self): + super().show_speed() + if self._speed > 60: + print('over speed') + + +class SportCar(Car): + pass + + +class WorkCar(Car): + def show_speed(self): + super().show_speed() + if self._speed > 40: + print('over speed') + + +class PoliceCar(Car): + _is_police = True + + +cars = [ + TownCar('Lada', 'grey'), + WorkCar('Belaz', 'orange'), + SportCar('Honda', 'black'), + PoliceCar('Opel', 'white') +] +turn_list = ['left', 'right', 'around'] +for car in cars: + print(car) + car.print_is_police() + car.go(random.randint(0, 60)) + car.go(random.randint(60, 240)) + car.turn(random.choice(turn_list)) + car.stop() + car.turn(random.choice(turn_list)) + print('') diff --git a/lesson-6/task5.py b/lesson-6/task5.py new file mode 100644 index 0000000..5046ae7 --- /dev/null +++ b/lesson-6/task5.py @@ -0,0 +1,28 @@ +class Stationery: + def __init__(self, title): + self.title = title + + def __str__(self): + return self.title + + def draw(self): + print('Запуск отрисовки') + + +class Pen(Stationery): + def draw(self): + print('Запуск отрисовки ручкой') + + +class Pencil(Stationery): + def draw(self): + print('Запуск отрисовки карандашом') + + +class Handle(Stationery): + def draw(self): + print('Запуск отрисовки маркером') + + +for st in (Pen('Pen'), Pencil('Pencil'), Handle('Handle')): + st.draw()