鱼C论坛

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

44讲的计时器问题

[复制链接]
发表于 2019-8-10 14:43:45 | 显示全部楼层 |阅读模式

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

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

x
import time as t

class Mytime():
    def __init__(self):
        self.prompt = '未开始计时!'
        self.lasted = []
        self.begin = 0
        self.end = 0
        self.unit = ['年','月','天','小时','分钟','秒']

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

    def __add__(self,other):
        prompt = '总共运行了'
        result = []
        for index in range(6):
            result.append(self.lasted[index]+other.lasted[index])
            if result[index]:
                prompt += (str(result[index])+self.unit[index])
        return prompt
   
    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()
            self._calc()
            print('结束计时...')

   
    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.unit[index])

        
  
        self.begin = 0
        self.end = 0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-11 20:15:27 | 显示全部楼层
防止没有调用stop属性
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-11 20:25:15 | 显示全部楼层
当你再次调用start()时,由于没有stop()被调用过,self.prompt就一直是这个内容,提示你计时还没结束。如果stop()被执行过了,那么self.prompt的内容就是实际的内容了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-17 13:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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