-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtask4.py
More file actions
80 lines (61 loc) · 1.65 KB
/
task4.py
File metadata and controls
80 lines (61 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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('')