简单定制
import time as tclass 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 __add__(self,other):
prompt = '总共运行了'
result = []
for index in range(6):
result.append(self.lasted+ other.lasted)
if result:
prompt += (str(result) + self.unit)
return prompt
#开始计时
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):#迭代计算各单位时间
lasted.append(self.end - self.begin)
def second(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 60 + lasted
def minute(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 60 + lasted
def hour(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 24 + lasted
def day(lasted):
if lasted < 0:
lasted = lasted -1
lasted = 31 + lasted
def mounth(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 12 + lasted
def consequence(self):#打印结果
self.prompt += str(self.lasted)
#为下一轮初始化准备
self.begin = 0
self.end = 0
print(self.prompt)
哪个大佬帮看看有什么错误没。
您自己先运行一下看看行不行 本帖最后由 huangdongdong 于 2021-4-21 19:59 编辑
1 柿子饼同学 发表于 2021-4-21 17:59
您自己先运行一下看看行不行
当我实例化stop是会报错,于是修改成此代码,但依旧会报错,没有思路。。 #设置计算方法
def second(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 60 + lasted
def minute(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 60 + lasted
def hour(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 24 + lasted
def day(lasted):
if lasted < 0:
lasted = lasted -1
lasted = 31 + lasted
def mounth(lasted):
if lasted < 0:
lasted = lasted - 1
lasted = 12 + lasted
#内部方法,计算运行时间
def _calc(self):
self.lasted = []
self.prompt = '总共运行了'
for index in range(6):#迭代计算各单位时间
self.lasted.append(self.end - self.begin)
self.second()
self.minute()
self.hour()
self.day
self.amounth
self.prompt += (str(self.lasted)) + self.unit
你这是要我的命啊,你这也太乱了 自己瞅瞅吧
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 __add__(self, other):
prompt = '总共运行了'
result = []
for index in range(6):
result.append(self.lasted + other.lasted)
if result:
prompt += (str(result) + self.unit)
return prompt
# 开始计时
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 - self.begin)
self.second()
self.minute()
self.hour()
self.day()
self.mounth()
self.prompt += (str(self.lasted)) + self.unit
def second(self):
if self.lasted < 0:
self.lasted = self.lasted - 1
self.lasted = 60 + self.lasted
def minute(self):
if self.lasted < 0:
self.lasted = self.lasted - 1
self.lasted = 60 + self.lasted
def hour(self):
if self.lasted < 0:
self.lasted = self.lasted - 1
self.lasted = 24 + self.lasted
def day(self):
if self.lasted < 0:
self.lasted = self.lasted - 1
self.lasted = 31 + self.lasted
def mounth(self):
if self.lasted < 0:
self.lasted = self.lasted - 1
self.lasted = 12 + self.lasted
def consequence(self):# 打印结果
for index in range(0, 6):
self.prompt += str(self.lasted)
# 为下一轮初始化准备
self.begin = 0
self.end = 0
print(self.prompt)
if __name__ == '__main__':
timer = Mytimer()
timer.start()
t.sleep(2)
timer.stop()
print(timer)
页:
[1]