鱼C论坛

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

[已解决]时间模块问题

[复制链接]
发表于 2018-3-3 11:00:18 | 显示全部楼层 |阅读模式

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

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

x
代码如下,在pycharm上运行

  1. import time as t

  2. class Timer:

  3.     def start(self):
  4.         self.start = t.localtime()
  5.         print("计时开始")

  6.     def stop(self):
  7.         self.stop = t.localtime()
  8.         self.calc()
  9.         print("计时结束")

  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)

  17. t = Timer()

  18. t.start()
复制代码


会爆出访问未知属性的错误,请问是为什么?不是已经引入时间模块了吗?

  1. Traceback (most recent call last):
  2.   File "/Users/williamwang/PycharmProjects/类和对象/venv/test1.py", line 24, in <module>
  3.     t.start()
  4.   File "/Users/williamwang/PycharmProjects/类和对象/venv/test1.py", line 6, in start
  5.     self.start = t.localtime()
  6. AttributeError: 'Timer' object has no attribute 'localtime'
复制代码
最佳答案
2018-3-3 11:15:02
a = Timer()

a.start()

不要用t,你已经用了时间模块,重复没搞混吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-3 11:15:02 | 显示全部楼层    本楼为最佳答案   
a = Timer()

a.start()

不要用t,你已经用了时间模块,重复没搞混吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-3 11:22:54 | 显示全部楼层

原来如此,还要注意不能重复调用,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-8 03:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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