|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面是小甲鱼里,最初版的计时器代码
=======================================
import time as t
class MyTimer():
def __init__(self):
self.until=['年','月','日','小时','分','秒']
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()
self.prompt="请先调用stop()结束计时!"
print("开始计时...")
#计时结束
def stop(self):
if not self.begin:
print('提示:请先调用start()方法')
else:
self.end=t.localtime()
print("计时结束!")
self._calc()
#计算时间
def _calc (self):
self.lasted=[]
self.prompt="总共运行了"
for index in range(6):
self.lasted.append(self.end[index]-self.begin[index])
if self.lasted[index]:
self.prompt+=(str(self.lasted[index])+self.until[index])
self.end=0
self.begin=0
#时间和
def __add__(self,other):
prompt=print("总共运行了")
result=[]
for index in range(6):
result.append(self.lasted[index]+other.lasted[index])
if result[index]:
prompt+=(str(result[index])+self.until[index])
return prompt
=====================================================
上面的是我自己写的,运行后 t1+t2 会报错,过程如下,实在是看不出来哪里有问题,求大神支招,谢谢
>>> t1=MyTimer()
>>> t1.start()
开始计时...
>>> t1.stop()
计时结束!
>>> t2=MyTimer()
>>> t2.start()
开始计时...
>>> t2.stop()
计时结束!
>>> t2
总共运行了7秒
>>> t1
总共运行了5秒
>>> t1+t2
总共运行了
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
t1+t2
File "C:/Users/Administrator/Desktop/代码库/4.23/MyTimer.py", line 46, in __add__
prompt+=(str(result[index])+self.until[index])
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
>>>
hi 原因已查明
bug在 41行
#时间和
def __add__(self,other):
prompt=print("总共运行了") #你将prompt错误赋值为 print()函数,导致 prompt 类型为 NoneType,修改为 prompt="总共运行了" 即可
|
|