|
|
发表于 2019-7-29 07:43:08
|
显示全部楼层
- #计时器
- import time as t
- class Mytimer():
- def __init__(self):
- self.unit = ['年','月','日','小时','分','秒']
- self.prompt = '未开始计时!'
- self.lasted = []
- self.begin = 0
- self.end = 0
- def __str__(self):
- return self.prompt
- __repr__ = __str__
-
- def start(self):
- self.begin = t.localtime #获取计时开始时当地时间
- self.prompt = '请先调用stop停止计时!'
- print('计时开始:')
-
- def stop(self):
- if not self.begin:
- print('请先调用start开始计时!')
- else:
- self.end = t.localtime #获取计时结束时当地时间
- self.__calc()
- print('计时结束')
-
- def __calc(self):
- self.lasted = []
- self.prompt = '一共运行了'
- for index in range(6):
- self.lasted.append(self.end[index] - self.begin[index])
- if self.lasted[index]:
- self.prompt += str(self.lasted[index]) + str(self.unit[index])
- self.begin = 0 #为下一轮初始化做准备
- self.end = 0
- a = Mytimer()
- a.start()
- a.stop()
复制代码 |
|