|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time as t
class Mytimer():
def __init__(self):
self.unit=['年','月','日','时','秒']
self.prompt='未开始计时'
self.begin=0
self.end=0
self.lasted=[]
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])+self.unit[index])
#print(self.prompt)
self.begin=0
self.end=0
执行结果:>>> t=Mytimer()
>>> t.stop()
提示:请先调用start函数,开始计时
>>> t.start()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
t.start()
File "C:\Users\xiaoqian\Desktop\时间重置计时.py", line 14, in start
self.begin = t.localtime()
AttributeError: 'Mytimer' object has no attribute 'localtime'
>>>
import time as t
class Mytimer():
def __init__(self):
self.unit=['年','月','日','时','分','秒']
self.prompt='未开始计时'
self.begin=0
self.end=0
self.lasted=[]
def __str__(self):
return self.prompt
__repr__=__str__
#计时开始
def start(self):
self.begin = t.localtime()
self.prompt='提示:请先调用stop函数,停止计时'
print(f'计时开始:')
#计时结束
def stop(self):
if not self.begin:
print('提示:请先调用start函数,开始计时')
else:
self.end = t.localtime()
self._calc()
print(f'计时结束:')
#时间计算
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])+self.unit[index])
print(self.prompt)
self.begin=0
self.end=0
改好了,记得运行时不要写 t = Mytimer()
|
|