CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day09/clock.py
Views: 729
1
from time import time, localtime, sleep
2
3
4
class Clock(object):
5
"""数字时钟"""
6
7
def __init__(self, hour=0, minute=0, second=0):
8
self._hour = hour
9
self._minute = minute
10
self._second = second
11
12
@classmethod
13
def now(cls):
14
ctime = localtime(time())
15
return cls(ctime.tm_hour, ctime.tm_min, ctime.tm_sec)
16
17
def run(self):
18
"""走字"""
19
self._second += 1
20
if self._second == 60:
21
self._second = 0
22
self._minute += 1
23
if self._minute == 60:
24
self._minute = 0
25
self._hour += 1
26
if self._hour == 24:
27
self._hour = 0
28
29
def show(self):
30
"""显示时间"""
31
return '%02d:%02d:%02d' % \
32
(self._hour, self._minute, self._second)
33
34
35
def main():
36
clock = Clock.now()
37
while True:
38
print(clock.show())
39
sleep(1)
40
clock.run()
41
42
43
if __name__ == '__main__':
44
main()
45
46