|

楼主 |
发表于 2018-7-30 21:54:02
|
显示全部楼层
本帖最后由 小思喵 于 2018-7-30 21:55 编辑
这是小甲鱼的 小甲鱼的代码是有时间差的 而我的却没有
- import time as t
- class Mytimer():
- #打印结果 :魔法方法 __str__和__repr__
- def __str__(self):
- return self.prompt
- __repr__ = __str__
- #定义普通方法:开始计时
- def start(self):
- self.start=t.localtime()
- print('开始计时')
- #定义普通方法:停止计时 并在结束的时候就得到过了多少时间
- def stop(self):
- self.stop=t.localtime()
- self._calu()
- print('停止计时')
- #定义内部方法(__):计算时间差:时间的年月日时分秒都是相互独立的
- #用序列来存储,用迭代来得到并计算
- def _calu(self):
- self.suboftime=[]#创建列表//得到的是 相差的年月日
- self.prompt='总共运行了'
- for index in range(6):#迭代计算得到年月日时分秒的差值 并用序列的方法append保存进序列
- self.suboftime.append(self.stop[index]-self.start[index]) #star.stop[index] 指的就是stop方法得到的时间的年/月/日/。。。
- self.prompt += str(self.suboftime[index])
- A=Mytimer()
- A.start()
- A.stop()
复制代码
|
|