44讲计时器
import time as timport calendar as c
class CalcTime:
def __init__(self):
self.begin_time = []
self.end_time = []
self.prompt = '未开始计时!'
self.unit = ['年', '月', '天', '时', '分', '秒']
self.long_list = []
def __str__(self):
return self.prompt
__repr__ = __str__
def __add__(self,other):
prompt = ''
result = []
for i in range(6):
result.append(self.long_list + other.longlist)
prompt += str(result) + self.unit
return self.prompt
def start(self):
self.prompt = '请先调用stop()停止计时!!'
self.begin_time = t.localtime()
print('计时开始...')
def stop(self):
if not self.begin_time:
print('请先调用start()开始计时!!')
else:
self.end_time = t.localtime()
self.cal()
print(self.long_list)
print('计时结束...')
def cal(self):
self.long_str = ''
self.moth = 0
self.index = 0
self.prompt = '总共运行了'
#得出两个时间点 年月日时分秒 相差的数值(可能为负)依次加入列表中
for i in range(6):
self.index = self.end_time - self.begin_time
self.long_list.append(self.index)
#若数值为负,处理后得出正确的时间列表
while True:
#月
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 12 + self.long_list
#日
if self.long_list < 0:
self.long_list = self.long_list - 1
#计算self.end_time当中的月份有多少天
self.month = (c.monthrange(self.end_time,self.end_time))
self.long_list = self.month + self.long_list
#时
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 24 + self.long_list
#分
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 60 + self.long_list
#秒
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 60 + self.long_list
break
for i in range(6):
self.prompt += str(self.long_list) + self.unit
#恢复初始和结束时间
self.begin_time = []
self.end_time = []
运行单个正常,但两个对象相加时始终报错(self.result.append(self.long_list + other.longlist)
AttributeError: 'CalcTime' object has no attribute 'longlist'),可longlist属性明明就有的啊
求大佬协助! longlist 少了个下划线,应该是 long_list 本帖最后由 Twilight6 于 2020-7-12 00:11 编辑
第一个错误: result.append(self.long_list + other.longlist) 的 longlist 少了个 下划线,改成 result.append(self.long_list + other.long_list)即可
第二个错误:__add__ 返回的是实例变量 self.prompt 而不是你 __add__ 里面设置的 prompt ,把 return self.prompt 改成 return prompt 即可
正确参考代码:
import time as t
import calendar as c
class CalcTime:
def __init__(self):
self.begin_time = []
self.end_time = []
self.prompt = '未开始计时!'
self.unit = ['年', '月', '天', '时', '分', '秒']
self.long_list = []
def __str__(self):
return self.prompt
__repr__ = __str__
def __add__(self, other):
prompt = ''
result = []
for i in range(6):
result.append(self.long_list + other.long_list)
print(result)
prompt += str(result) + self.unit
return prompt
def start(self):
self.prompt = '请先调用stop()停止计时!!'
self.begin_time = t.localtime()
print('计时开始...')
def stop(self):
if not self.begin_time:
print('请先调用start()开始计时!!')
else:
self.end_time = t.localtime()
self.cal()
print(self.long_list)
print('计时结束...')
def cal(self):
self.long_str = ''
self.moth = 0
self.index = 0
self.prompt = '总共运行了'
# 得出两个时间点 年月日时分秒 相差的数值(可能为负)依次加入列表中
for i in range(6):
self.index = self.end_time - self.begin_time
self.long_list.append(self.index)
# 若数值为负,处理后得出正确的时间列表
while True:
# 月
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 12 + self.long_list
# 日
if self.long_list < 0:
self.long_list = self.long_list - 1
# 计算self.end_time当中的月份有多少天
self.month = (c.monthrange(self.end_time, self.end_time))
self.long_list = self.month + self.long_list
# 时
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 24 + self.long_list
# 分
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 60 + self.long_list
# 秒
if self.long_list < 0:
self.long_list = self.long_list - 1
self.long_list = 60 + self.long_list
break
for i in range(6):
self.prompt += str(self.long_list) + self.unit
# 恢复初始和结束时间
self.begin_time = []
self.end_time = []
在答案出来之前我已经找到了,还是感谢各位大佬!么么哒
页:
[1]