鱼C论坛

 找回密码
 立即注册
查看: 1692|回复: 2

代码报错求帮助解决

[复制链接]
发表于 2021-6-16 14:55:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 fc5igm 于 2021-6-16 15:47 编辑
  1. import time
  2. class MyTimer:
  3.     def start(self):
  4.         self.start=time.time()
  5.         print('计时开始!')
  6.     def stop(self):
  7.         self.stop=time.time()
  8.         try:
  9.             self.lstm=round(float(self.stop)-(self.start))
  10.             print('计时结束!')
  11.         except (AttributeError,TypeError):
  12.             print('提示:请先调用start()开始计时!')
  13.     def __repr__(self):
  14.         try:
  15.             return f'总共运行了{self.lstm}秒'
  16.         except (AttributeError,TypeError):
  17.             return '未开始计时!'
  18.     def __str__(self):
  19.         try:
  20.             return f'总共运行了{self.lstm}秒'
  21.         except (AttributeError,TypeError):
  22.             return '未开始计时!'
  23.     def __add__(self,other):
  24.         print(int(self.lstm)+int(other.lstm))
  25. t1=MyTimer()
复制代码
  1. >>> t1.start()
  2. 计时开始!
  3. >>> t1.stop()
  4. 计时结束!
  5. >>> t1
  6. 总共运行了4秒
  7. >>> t1.start()
  8. Traceback (most recent call last):
  9.   File "<pyshell#37>", line 1, in <module>
  10.     t1.start()
  11. TypeError: 'float' object is not callable
  12. >>> t1.start
  13. 1623825866.1460986
  14. >>> t1.start()
  15. Traceback (most recent call last):
  16.   File "<pyshell#39>", line 1, in <module>
  17.     t1.start()
  18. TypeError: 'float' object is not callable
复制代码

为什么t1.start()第一次调用就成功,第二次则报错了?另外,'float' object is not callable是什么意思?
我换成str字符串也报了一样的错误。那什么样子才是callable的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-16 15:40:50 | 显示全部楼层
  1. import time as t
  2. class MyTimer():
  3.     def start(self):
  4.         self.start=t.localtime()
  5.         print('计时开始')
  6.     def stop(self):
  7.         self.stop=t.localtime()
  8.         print('计时结束')
  9.         self._calc()
  10.     def _calc(self):
  11.         self.lasted=[]
  12.         self.prompt='总共运行了'
  13.         for index in range(6):
  14.             self.lasted.append(self.stop[index]-self.start[index])
  15.             self.prompt+=str(self.lasted[index])
  16.         print(self.prompt)
复制代码
  1. >>> t1=MyTimer()
  2. >>> t1.start()
  3. 计时开始
  4. >>> t1.stop()
  5. 计时结束
  6. 总共运行了00001-57
  7. >>> t1.start()
  8. Traceback (most recent call last):
  9.   File "<pyshell#68>", line 1, in <module>
  10.     t1.start()
  11. TypeError: 'time.struct_time' object is not callable
  12. >>> t1.start()
  13. Traceback (most recent call last):
  14.   File "<pyshell#69>", line 1, in <module>
  15.     t1.start()
  16. TypeError: 'time.struct_time' object is not callable
复制代码

小甲鱼课上代码也出现了同样的问题。如果一个实例对象属性被赋值过一次,后边就不可以再被赋值第二次了么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-6-16 15:47:25 | 显示全部楼层
忘记属性和方法不能重名了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 20:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表