鱼C论坛

 找回密码
 立即注册
查看: 2072|回复: 7

44看了视频后自己动手写 查不出到底是哪里错了 求点醒!!

[复制链接]
发表于 2017-2-24 05:12:44 | 显示全部楼层 |阅读模式

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

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

x
import time as t
class Timer():
    def __init__(self):
        self.units=['Year','Month','Day','hour','minute','second']
        self.prompt='the timer is yet to start'
        self.begin=0
        self.end=0
        self.lasted=[]
    def __str__(self):
        return self.prompt
    __repr__=__str__
    def ___add__(self,other):
        result=[]
        prompt='two of them totally took '
        for index in range(6):
            result.append(self.lasted[index]+other.lasted[index])
            if result[index]:
                prompt+=(str(result[index])+self.units[index])
        return prompt
         
        
    def start(self):
        self.begin=t.localtime()
        self.prompt='Please call the stop function to stop'
        print ('Timer starts right now!')
    def stop(self):
        if not self.begin:
            print ('Please call the start funcation to start the timer.')
        else:
            self.end=t.localtime()
            self.__calc()
            print ('The timer stops now.')
    def __calc(self):
        self.lasted=[]
        self.prompt='It took '
        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.units[index])
        
        self.begin=0
        self.end=0
错误提示:
>>> t1+t2
Traceback (most recent call last):
  File "<pyshell#202>", line 1, in <module>
    t1+t2
TypeError: unsupported operand type(s) for +: 'Timer' and 'Timer'

我快哭了,想了一个小时也没想明白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-24 08:19:35 | 显示全部楼层
def ___add__(self,other):
这句改成
def __add__(self,other):
试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-24 19:11:25 | 显示全部楼层
  1. import time as t
  2. class Timer():
  3.     def __init__(self):
  4.         self.units=['Year','Month','Day','hour','minute','second']
  5.         self.prompt='the timer is yet to start'
  6.         self.begin=0
  7.         self.end=0
  8.         self.lasted=[]
  9.     def __str__(self):
  10.         return self.prompt
  11.     __repr__=__str__
  12.     def ___add__(self,other):
  13.         result=[]
  14.         prompt='two of them totally took '
  15.         for index in range(6):
  16.             result.append(self.lasted[index]+other.lasted[index])
  17.             if result[index]:
  18.                 prompt+=(str(result[index])+self.units[index])
  19.         return prompt
  20.          
  21.         
  22.     def start(self):
  23.         self.begin=t.localtime()
  24.         self.prompt='Please call the stop function to stop'
  25.         print('Timer starts right now!')
  26.     def stop(self):
  27.         if not self.begin:
  28.             print('Please call the start funcation to start the timer.')
  29.         else:
  30.             self.end=t.localtime()
  31.             self.__calc()
  32.             print('The timer stops now.')
  33.     def __calc(self):
  34.         self.lasted=[]
  35.         self.prompt='It took '
  36.         for index in range(6):
  37.             self.lasted.append(self.end[index]-self.begin[index])
  38.             if self.lasted[index]:
  39.                 self.prompt+=(str(self.lasted[index])+self.units[index])
  40.         
  41.         self.begin=0
  42.         self.end=0
复制代码


同志别哭,冷静
我不太明白你写的代码
我建议你和小甲鱼的对一下
你定义的一些函数没有被调用
我看了看,运行了一下
没有任何输出结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-24 22:43:16 | 显示全部楼层
EdenRuo 发表于 2017-2-24 08:19
def ___add__(self,other):
这句改成
def __add__(self,other):

谢大神火眼金睛!!!么么哒!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-2-24 22:44:17 | 显示全部楼层
新手·ing 发表于 2017-2-24 19:11
同志别哭,冷静
我不太明白你写的代码
我建议你和小甲鱼的对一下

谢谢您热心回复 第一次发帖感受到了鱼c论坛上的前辈们温暖
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-25 09:01:21 | 显示全部楼层
lovemyisa 发表于 2017-2-24 22:44
谢谢您热心回复 第一次发帖感受到了鱼c论坛上的前辈们温暖

我也不是前辈
也只是个小白
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-25 11:04:11 From FishC Mobile | 显示全部楼层
我也写过这个,我的在修改之后就跑不了了,没看出哪里有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-7 05:10:33 | 显示全部楼层
fei371203 发表于 2017-2-25 11:04
我也写过这个,我的在修改之后就跑不了了,没看出哪里有问题

二楼的回复已经帮我解决问题了,多打了一个下划线
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 10:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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