鱼C论坛

 找回密码
 立即注册
查看: 2760|回复: 4

[已解决]新手问题求助

[复制链接]
发表于 2018-9-27 18:33:42 | 显示全部楼层 |阅读模式

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

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

x
这个是报错是什么原因: t1=MyTimer()
Traceback (most recent call last):
  File "<pyshell#211>", line 1, in <module>
    t1=MyTimer()
  File "C:/Users/lenovo/Desktop/计时器(秒).py", line 9, in __init__
    self.end=0
TypeError: cannot unpack non-iterable builtin_function_or_method object

代码是我按照小甲鱼的课后题手动输入的,自己找不到问题。代码如下:import time as t

class MyTimer:
    def __init__(self):
        
        self.prompt='计时未开始!'
        self.lasted=0.0
        self.begin=0
        self.end=0
        self.default_timer=t.perf_counter

    def __str__(self):
        return self.prompt
   
    __repr__=__str__

    def __add__(self,other):
        result=self.lasted+other.lasted
        prompt = '总共运行了%0.2f秒'%result
        return prompt

   
    #开始计时
    def start(self):
        self.begin=self.defailt_timer()
        self.prompt='提示:请先调用stop()停止计时!'
        print('计时开始')

    #停止计时
    def stop(self):
        if not self.begin:
            print('提示:请先调用start()开始计时')
            
        else:
            self.end=self.default_timer()
            self._calc()
            print('计时结束!')
            

    #内部方法,计算运行时间
    def _calc(self):
        self.lasted=self.end - self.begin
        self.prompt='总共运行了%0.2f秒'% self.lasted
        
                                   
        #为下一轮计算准备
        self.begin=0
        self.end=0

        #设置计时器(time.perf_counter()或time.process_time())
        def set_timer(self,timer):
            if timer=='process_time':
                self.default_timer=t.process_time
            elif timer == 'perf_counter':
                self.default_timer=t.perf_counter
            else:
                print ('输入无效,请输入perf_counter或者process_time')
最佳答案
2018-9-27 18:57:00
吃鱼的虾 发表于 2018-9-27 18:53
我给它实例化的时候就报错为什么

你的代码是不是和粘贴出来的不一样
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-27 18:45:57 | 显示全部楼层
没出现你说的错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-27 18:53:37 | 显示全部楼层
塔利班 发表于 2018-9-27 18:45
没出现你说的错误

我给它实例化的时候就报错为什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-27 18:57:00 | 显示全部楼层    本楼为最佳答案   
吃鱼的虾 发表于 2018-9-27 18:53
我给它实例化的时候就报错为什么

你的代码是不是和粘贴出来的不一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-27 18:58:53 | 显示全部楼层
塔利班 发表于 2018-9-27 18:57
你的代码是不是和粘贴出来的不一样

可能是我上先前运行的代码的原因吧,我重启了一下IDLE就Ok就ok了,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-14 02:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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