|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
python 44课 课堂.....(写一个计时器类)
- import time
- class mytime:
- def __init__(self):
- self.unit=['年','月','日','小时','分钟','秒']
- self.start = 0
- self.stop = 0
- self.lasted = []
- self.instance = '未开始计时'
-
- def __str__(self):
- return self.instance
- __repr__ = __str__
- [color=DarkOrange] def __add__(self,other):
- alltime = []
- instance = '共运行了'
- for count2 in range(6):
- alltime.append(self.lasted[count2]+other.lasted[count2])
- if alltime[count2] != 0:
- instance+=(str(alltime[count2])+self.unit(count2))
- return instance
- [/color]
- return instance
- #开始计时
- def begin(self):
- self.start = time.localtime()
- print('您于%d分,%d秒开始计时'% (self.start[4],self.start[5]))
- #结束计时
- def end(self):
- if self.start == 0:
- print('请先启动begin()程序')
- else:
- self.stop = time.localtime()
- self._last1()
- print('计时结束')
- #对象本身的方法
- def _last1(self):
- self.lasted = []
- self.instance = '共持续了'
- for count in range(6):
- self.lasted.append(self.stop[count] - self.start[count])
- if self.lasted[count]!=0:
- self.instance += str(self.lasted[count])+self.unit[count]
-
- #初始化
- self.start = 0
- self.stop = 0
-
复制代码
问题主要在 __add__(黄色) 那一块
结果:- >>> a = mytime()
- >>> a.begin()
- 您于35分,23秒开始计时
- >>> a.end()
- 计时结束
- >>> a
- 共持续了4秒
- >>> b = mytime()
- >>> b.begin()
- 您于35分,46秒开始计时
- >>> b.end()
- 计时结束
- >>> b
- 共持续了3秒
- >>> a+b
- Traceback (most recent call last):
- File "<pyshell#8>", line 1, in <module>
- a+b
- File "D:\颖哥哥的小宝库\p .ce4.py", line 19, in __add__
- instance+=(str(alltime[count2])+self.unit(count2))
- [color=DarkOrange]TypeError: 'list' object is not callable
- [/color]>>>
复制代码
我主要也是跟着小甲鱼一起写写先练练手,可是为什么这块问题.......
请大神解答
第19行
instance+=(str(alltime[count2])+self.unit[count2])
|
|