-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.py
More file actions
42 lines (35 loc) · 1.08 KB
/
Timer.py
File metadata and controls
42 lines (35 loc) · 1.08 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
def format_time(h, m, s):
return f"{h:02}:{m:02}:{s:02}"
class Timer:
def __init__(self, hours=0, minutes=0, seconds=0):
self.__hours = hours
self.__minutes = minutes
self.__seconds = seconds
def __str__(self):
return format_time(self.__hours, self.__minutes, self.__seconds)
def next_second(self):
self.__seconds += 1
if self.__seconds == 60:
self.__seconds = 0
self.__minutes += 1
if self.__minutes == 60:
self.__minutes = 0
self.__hours += 1
if self.__hours == 24:
self.__hours = 0
def prev_second(self):
self.__seconds -= 1
if self.__seconds == -1:
self.__seconds = 59
self.__minutes -= 1
if self.__minutes == -1:
self.__minutes = 59
self.__hours -= 1
if self.__hours == -1:
self.__hours = 23
timer = Timer(23, 59, 59)
print(timer)
timer.next_second()
print(timer)
timer.prev_second()
print(timer)