-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter
56 lines (45 loc) · 1.27 KB
/
Counter
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
class Counter():
def __init__(self, current=1, min_value=0, max_value=10):
self.current = current
self.min_value = min_value
self.max_value = max_value
self.max_max = 0
self.min_min = 0
def set_current(self, start):
self.current = start
def set_max(self, max_max):
self.max_max = max_max
def set_min(self, min_min):
self.min_min = min_min
def step_up(self):
if self.current >= self.max_max:
raise ValueError("Maximum value")
self.current += 1
def step_down(self):
if self.current <= self.min_min:
raise ValueError("Minimum value")
self.current -= 1
def get_current(self):
return self.current
counter = Counter()
counter.set_max(20)
counter.set_current(7)
counter.step_up()
counter.step_up()
counter.step_up()
print(counter.get_current()) # 10
try:
counter.step_up() # ValueError
except ValueError as e:
print(e) # Достигнут максимум
print(counter.get_current()) # 10
counter.set_min(7)
counter.step_down()
counter.step_down()
counter.step_down()
print(counter.get_current()) # 7
try:
counter.step_down() # ValueError
except ValueError as e:
print(e) # Достигнут минимум
print(counter.get_current()) # 7