|
10鱼币
运行代码:import time as t
class Mytimer:
t1 = []
t2 = []
t3 = []
t4 = []
def start(self):
self.t1.append(t.strftime("%H",t.localtime()))
self.t1.append(t.strftime("%M",t.localtime()))
self.t1.append(t.strftime("%S",t.localtime()))
print('开始计时……')
def stop(self):
self.t2.append(t.strftime("%H",t.localtime()))
self.t2.append(t.strftime("%M",t.localtime()))
self.t2.append(t.strftime("%S",t.localtime()))
self._calc()
def _calc(self):
for i in range(3):
if self.t1 == []:
print("请先调用start函数")
break
else:
self.t3.append(int(self.t2[i]) - int(self.t1[i]))
#此处有0时59分和1时的问题,相减没有退位的问题
print("停止计时")
print("总共运行了{}时{}分{}秒".format(self.t3[0],self.t3[1],self.t3[2]))
def __add__(self,other):
for i in range(3):
self.t4.append(self.t3[i] + other.t3[i])
print("总共运行了{}时{}分{}秒".format(self.t4[0],self.t4[1],self.t4[2]))
self.t1 = []
self.t2 = []
self.t3 = []
结果显示:
>>> t3 = Mytimer()
>>> t3.start()
开始计时……
>>> t3.stop()
停止计时
总共运行了0时0分10秒
>>> t4 = Mytimer()
>>> t4.start()
开始计时……
>>> t4.stop()
停止计时
总共运行了0时0分10秒
>>> t3 + t4
总共运行了0时0分20秒
多次试验红色部分永远一样,是哪里出问题了?
本帖最后由 2012277033 于 2021-8-10 12:21 编辑
你这个代码问题太多了,大致猜测你应该将time重命名为t了,按照你的思路,大概改了下: - import time as t
- class Mytimer:
- def __init__(self):
- self.t1 = []
- self.t2 = []
- self.t3 = []
- self.t4 = []
- def start(self):
- self.t1.append(t.strftime("%H",t.localtime()))
- self.t1.append(t.strftime("%M",t.localtime()))
- self.t1.append(t.strftime("%S",t.localtime()))
- print('开始计时……')
- def stop(self):
- self.t2.append(t.strftime("%H",t.localtime()))
- self.t2.append(t.strftime("%M",t.localtime()))
- self.t2.append(t.strftime("%S",t.localtime()))
- self._calc()
- def _calc(self):
- if self.t1 == []:
- print("请先调用start函数")
- return
- else:
- #这里这么计算,是当比如说11:53:57 到11:54:02的时候,产生负数,就要借一
- self.t3.append(int(self.t2[2]) - int(self.t1[2]))
- if self.t3[0]<0:
- self.t3[0]+=60
- self.t2[1]=str(int(self.t2[1])-1)
- self.t3.insert(0,int(self.t2[1]) - int(self.t1[1]))
- if self.t3[0]<0:
- self.t3[0]+=60
- self.t2[0]=str(int(self.t2[0])-1)
- self.t3.insert(0,int(self.t2[0]) - int(self.t1[0]))
-
- print("停止计时")
- print("总共运行了{}时{}分{}秒".format(self.t3[0],self.t3[1],self.t3[2]))
- def __add__(self,other):
- self.t4.append(self.t3[0]+other.t3[0])
- self.t4.append(self.t3[1]+other.t3[1])
- if self.t4[1]>=60:
- self.t4[1]=60-self.t4[1]
- self.t4[0]+1
- self.t4.append(self.t3[2]+other.t3[2])
- if self.t4[2]>=60:
- self.t4[2]=60-self.t4[2]
- self.t4[1]+1
- if self.t4[1]>=60:
- self.t4[1]=60-self.t4[1]
- self.t4[0]+1
- print("总共运行了{}时{}分{}秒".format(self.t4[0],self.t4[1],self.t4[2]))
- self.t1 = []
- self.t2 = []
- self.t3 = []
- t3 = Mytimer()
- t2 = Mytimer()
- t3.start()
- t.sleep(2)
- t3.stop()
- t2.start()
- t.sleep(2)
- t2.stop()
- t2+t3
复制代码
不过,其实实现计时器可以更简单点:
- import time as time
- class Mytimer:
- def __init__(self):
- self.starttime=0
- self.count=0
- def start(self):
- self.starttime=int(time.time())
- print('开始计时……')
- def stop(self):
- self.count=int(time.time())-self.starttime
- print("共运行了{}时{}分{}秒".format(self.count//3600,self.count//60%60,self.count%60))
- def __add__(self,other):
- temp = self.count+other.count
- print("共运行了{}时{}分{}秒".format(temp//3600,temp//60%60,temp%60))
- self.starttime=0
- self.count=0
- t3 = Mytimer()
- t2 = Mytimer()
- t3.start()
- time.sleep(2)
- t3.stop()
- t2.start()
- time.sleep(2)
- t2.stop()
- t2+t3
复制代码
|
最佳答案
查看完整内容
你这个代码问题太多了,大致猜测你应该将time重命名为t了,按照你的思路,大概改了下:
不过,其实实现计时器可以更简单点:
|