|
发表于 2020-4-4 23:28:58
|
显示全部楼层
本帖最后由 十月故里 于 2020-4-4 23:36 编辑
首先,吐槽一下你代码抄写的能力。。。第30行,‘-’写成‘=’,
31行也是str(self.lasted[index]) + self.unit[index] 写成 str(self.lasted = [index] + self.unit[index])
多了‘=’而且,int类不能和str类使用拼接方法。。。。
其次,你要隐藏为0的结果方法就是四楼说的。在你对self.prompt赋值前添加条件即可
最后给你改的代码(ps:你弄的代码好歹把import的time模块加上惹)
- import time as t
- class MyTime():
- 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()
- print("计时开始...")
- #停止计时
- def stop(self):
- 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[-1]:
- self.prompt += str(self.lasted[index]) + self.unit[index]
复制代码
上面这段代码仅供参考哈,只是给你一个思路,里面有个细节还没处理,你自己弄一下,就是time.localtime模块返回的时间做差之后会存在负值的情况,你得处理一下数据才行哦
|
|