-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_OPP.py
More file actions
47 lines (40 loc) · 1.21 KB
/
timer_OPP.py
File metadata and controls
47 lines (40 loc) · 1.21 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
def two_digits(val):
s = str(val)
if len(s) == 1:
s = '0' + s
return s
class Timer:
def __init__(self, hours=0, minutes=0, seconds=0):
self.__hours = hours
self.__minutes = minutes
self.__seconds = seconds
def __str__(self):
return two_digits(self.__hours) + ":" + \
two_digits(self.__minutes) + ":" + \
two_digits(self.__seconds)
def next_second(self):
self.__seconds += 1
if self.__seconds > 59:
self.__seconds = 0
self.__minutes += 1
if self.__minutes > 59:
self.__minutes = 0
self.__hours += 1
if self.__hours > 23:
self.__hours = 0
def prev_second(self):
self.__seconds -= 1
if self.__seconds < 0:
self.__seconds = 59
self.__minutes -= 1
if self.__minutes < 0:
self.__minutes = 59
self.__hours -= 1
if self.__hours < 0:
self.__hours = 23
timer = Timer(23, 59, 59)
print(timer)
timer.next_second()
print(timer)
timer.prev_second()
print(timer)