鱼C论坛

 找回密码
 立即注册
查看: 1299|回复: 1

[已解决]初学定制计时器类,出现的问题

[复制链接]
发表于 2018-4-24 00:02:22 | 显示全部楼层 |阅读模式

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

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

x
下面是小甲鱼里,最初版的计时器代码
=======================================

import time as t
class MyTimer():
    def __init__(self):
        self.until=['年','月','日','小时','分','秒']
        self.prompt='计时未开始!'
        self.lasted=[]
        self.begin=0
        self.end=0
    def __str__(self):
        return self.prompt
    __repr__=__str__
   
    #开始计时
    def start(self):
        self.begin=t.localtime()
        self.prompt="请先调用stop()结束计时!"
        print("开始计时...")
        
    #计时结束
    def stop(self):
        if  not self.begin:
            print('提示:请先调用start()方法')
        else:
            self.end=t.localtime()   
            print("计时结束!")
            self._calc()
        
    #计算时间
    def _calc (self):
        self.lasted=[]
        self.prompt="总共运行了"
        for index in range(6):
            self.lasted.append(self.end[index]-self.begin[index])
            if self.lasted[index]:
                self.prompt+=(str(self.lasted[index])+self.until[index])
        self.end=0
        self.begin=0

    #时间和
    def __add__(self,other):
        prompt=print("总共运行了")
        result=[]
        for index in range(6):
            result.append(self.lasted[index]+other.lasted[index])
            if result[index]:
                prompt+=(str(result[index])+self.until[index])
        return prompt

=====================================================

上面的是我自己写的,运行后  t1+t2  会报错,过程如下,实在是看不出来哪里有问题,求大神支招,谢谢
   
>>> t1=MyTimer()
>>> t1.start()
开始计时...
>>> t1.stop()
计时结束!
>>> t2=MyTimer()
>>> t2.start()
开始计时...
>>> t2.stop()
计时结束!
>>> t2
总共运行了7秒
>>> t1
总共运行了5秒
>>> t1+t2
总共运行了
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    t1+t2
  File "C:/Users/Administrator/Desktop/代码库/4.23/MyTimer.py", line 46, in __add__
    prompt+=(str(result[index])+self.until[index])
TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str'
>>>      
            
        
最佳答案
2018-4-24 08:56:21
hi 原因已查明

bug在 41行

#时间和
    def __add__(self,other):
        prompt=print("总共运行了") #你将prompt错误赋值为 print()函数,导致 prompt 类型为 NoneType,修改为 prompt="总共运行了"    即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-24 08:56:21 | 显示全部楼层    本楼为最佳答案   
hi 原因已查明

bug在 41行

#时间和
    def __add__(self,other):
        prompt=print("总共运行了") #你将prompt错误赋值为 print()函数,导致 prompt 类型为 NoneType,修改为 prompt="总共运行了"    即可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 17:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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