Skip to content
Open

HW6 #115

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lesson-6/task1.py
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 11 additions & 0 deletions lesson-6/task2.py
Original file line number Diff line number Diff line change
@@ -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())
23 changes: 23 additions & 0 deletions lesson-6/task3.py
Original file line number Diff line number Diff line change
@@ -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())
80 changes: 80 additions & 0 deletions lesson-6/task4.py
Original file line number Diff line number Diff line change
@@ -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('')
28 changes: 28 additions & 0 deletions lesson-6/task5.py
Original file line number Diff line number Diff line change
@@ -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()