妖_孽 发表于 2018-8-8 21:16:06

第45讲简单定制问题

在看视频的时候按照小甲鱼写的代码,自己敲出来之后却出错了
已经仔细看过了,并没有语法格式上的错误
就是调用开始计时和结束计时,然后计算时间差,实现一个简单的计时器
代码如下
import time as t

class MyTimer():
    #开始计时
    def start(self):
      self.start = t.localtime()
      print("计时开始")

    #停止计时
    def stop(self):
      self.stop = t.localtime()
      self._clac()
      print("计时结束")

    #内部方法 计算运行时间
    def _calc(self):
      self.lasted = []
      self.prompt = "总共运行了"
      for index in range(6):
            self.lasted.append(self.stop - self.start)
            self.prompt += str(self.lasted)

      print(self.prompt)


调用及错误如下
>>> tt = MyTimer()
>>> tt.stop()
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
    tt.stop()
File "D:/Python/fishc_study/45.2.py", line 12, in stop
    self._clac()
AttributeError: 'MyTimer' object has no attribute '_clac'
>>>

刚开始学python 许多不清楚的地方 还请各位过帮助帮助

claws0n 发表于 2018-8-8 21:25:32

自己打错字。第12行 self._calc()
页: [1]
查看完整版本: 第45讲简单定制问题