鱼C论坛

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

[已解决]44讲课堂练习

[复制链接]
发表于 2020-8-4 18:21:04 | 显示全部楼层 |阅读模式

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

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

x
  1. <div>import time as t</div><div>class Time():
  2.    
  3.     #开始计时
  4.     def start(self):
  5.         self.start=t.localtime
  6.         print('计时开始')
  7.         
  8.     #停止计时
  9.     def stop(self):
  10.         self.stop=t.localtime
  11.         self._calc()
  12.         print('计时结束')
  13.         
  14.     #内部方法,计算时间
  15.     def _calc(self):
  16.         self.lasted=[]
  17.         self.prompt="总共运行了"
  18.         for index in range(6):
  19.             self.lasted.append(self.stop[index]-self.start[index])
  20.             self.prompt +=str(self.lasted[index])</div><div>        print(self.prompt)

  21. </div>
复制代码
  1. ======================== RESTART: E:/python/程序/44_计时器.py =======================
  2. >>> q=Time()
  3. >>> q.start()
  4. 计时开始
  5. >>> q.stop()
  6. Traceback (most recent call last):
  7.   File "<pyshell#26>", line 1, in <module>
  8.     q.stop()
  9.   File "E:/python/程序/44_计时器.py", line 13, in stop
  10.     self._calc()
  11.   File "E:/python/程序/44_计时器.py", line 21, in _calc
  12.     self.lasted.append(self.stop[index]-self.start[index])
  13. TypeError: 'builtin_function_or_method' object is not subscriptable
复制代码

不知道为什么错了?并且想知道怎么改?



最佳答案
2020-8-4 19:07:10
本帖最后由 sunrise085 于 2020-8-4 19:08 编辑

共有三处两种错误,帮你修改了,在注释中已经写出来了。
1.类变量名与类函数名重复了
2.localtime是函数,调用函数需要加括号

  1. import time as t
  2. class Time():
  3.    
  4.     #开始计时
  5.     def start(self):
  6.         self.begin=t.localtime()#1.这里类变量名与类函数名重复了;2.localtime是函数名,需要加括号才是调用函数
  7.         print('计时开始')
  8.         
  9.     #停止计时
  10.     def stop(self):
  11.         self.end=t.localtime()#1.这里类变量名与类函数名重复了;2.localtime是函数名,需要加括号才是调用函数
  12.         self._calc()
  13.         print('计时结束')
  14.         
  15.     #内部方法,计算时间
  16.     def _calc(self):
  17.         self.lasted=[]
  18.         self.prompt="总共运行了"
  19.         for index in range(6):
  20.             self.lasted.append(self.end[index]-self.begin[index])#stop和start同样需要改为end和begin
  21.             self.prompt +=str(self.lasted[index])
  22.         print(self.prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-4 18:49:20 | 显示全部楼层
本帖最后由 1q23w31 于 2020-8-4 18:52 编辑
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Aug  4 09:24:02 2020

  4. @author: Administrator
  5. """
  6. import time as t
  7. class Time():
  8.     def __init__(self):
  9.         self.start = ''
  10.         self.stop = ''
  11.    
  12.     #开始计时
  13.     def kaishi(self):
  14.         self.start=t.time()
  15.         print('计时开始')
  16.         
  17.     #停止计时
  18.     def jiesu(self):
  19.         self.stop=t.time()
  20.         self._calc()
  21.         print('计时结束')
  22.         
  23.     #内部方法,计算时间
  24.     def _calc(self):
  25.         self.lasted=[]
  26.         self.prompt="总共运行了"
  27.         self.lasted.append(self.stop-self.start)
  28.         self.prompt +=str(self.lasted)
  29.         print(self.prompt)

复制代码



                               
登录/注册后可看大图


1、变量名与函数名重复
2、time那l没调用
3、localtime返回的对象不支持相减
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-4 19:07:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-8-4 19:08 编辑

共有三处两种错误,帮你修改了,在注释中已经写出来了。
1.类变量名与类函数名重复了
2.localtime是函数,调用函数需要加括号

  1. import time as t
  2. class Time():
  3.    
  4.     #开始计时
  5.     def start(self):
  6.         self.begin=t.localtime()#1.这里类变量名与类函数名重复了;2.localtime是函数名,需要加括号才是调用函数
  7.         print('计时开始')
  8.         
  9.     #停止计时
  10.     def stop(self):
  11.         self.end=t.localtime()#1.这里类变量名与类函数名重复了;2.localtime是函数名,需要加括号才是调用函数
  12.         self._calc()
  13.         print('计时结束')
  14.         
  15.     #内部方法,计算时间
  16.     def _calc(self):
  17.         self.lasted=[]
  18.         self.prompt="总共运行了"
  19.         for index in range(6):
  20.             self.lasted.append(self.end[index]-self.begin[index])#stop和start同样需要改为end和begin
  21.             self.prompt +=str(self.lasted[index])
  22.         print(self.prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 20:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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