python也玩私人定制。一个小的计时器

import time as t
class Time():    
    def __str__(self):        
    return  self.prompt    
__repr__ = __str__    
def __iadd__(self, other):        
    tips='相加后共运行了'        
    result = []       
    print(other.lasted)        
    print(self.lasted)        
    for index in range(6):            
    result.append(self.lasted[index] + other.lasted[index])                                                  
    print(index)            
    if result[index]:                
         tips +=(str(result[index])+self.un[index])       
         return tips   
 #      定义参数    
def __init__(self):       
    self.begin = 0        
    self.end = 0        
    self.lasted = []        
    self.prompt = '还没开始即使啦。先用start好吗?'        
    self.un=['年','月','日','时','分','秒']
# 开始计时    
def start(self):              
    self.gegin = t.localtime()        
    print('开始计时')
#结束计时    
def stop(self):            
    self.end = t.localtime()            
    if self.begin == 0:                
          print('请先调用start()方法!!!')           
    else:               
          self.calc()                
          print('停止计时')#计算运行时间    
def calc(self):        
    self.prompt = '总共运行了'        
    lasted = []        
    for index in range(6) :            
    lasted.append(self.end[index]-self.begin[index])            
    if lasted[index]:                
          self.prompt += (str(lasted[index]) + self.un[index])                        
          print(list(lasted))       
    self.begin=0        
    self.end=0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容