|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 林斌 于 2016-4-1 18:24 编辑
- import time as t
- class MyTimer:
- # 初始化
- def __init__(self):
- self.begin = 0
- self.end = 0
- self.prompt = "啥都没干,让我显示什么,赶紧输入 **.start()"
- self.danwei = ["年", "月", "日", "时", "分", "秒"]
- self.liangzhe = []
- self.zhuanhua = ['', 12, 30, 24, 60, 60]
- # 打印 显示
- def __str__(self):
- return self.prompt
- #两者相加
- def __add__(self, other):
- self.prompt = "总共花费"
- for i in range(6):
- self.liangzhe.append(self.lasted[i] + other.lasted[i])
- self.zhuanhuan(self.liangzhe)
- # print(self.liangzhe)
- return self.prompt
- __repr__ = __str__
- # 开始计时
- def start(self):
- self.begin = t.localtime()
- print("开始计时")
- # 计时结束
- def stop(self):
- if not self.begin:
- print("提示:请先调用 start()开始计时!")
- else:
- self.end = t.localtime()
- self._jisuan()
- print("计时结束")
- # 定义内部方法,进行时间计算
- def _jisuan(self):
- self.lasted = []
- self.prompt = "总共花费"
- for index in range(6):
- self.lasted.append(self.end[index] - self.begin[index])
- # print(self.lasted)
- self.zhuanhuan(self.lasted)
- # 定义一个转换函数(将时间清晰化)
-
- def zhuanhuan(self, x):
- # print("执行转换函数了!!")
- # print(x)
- self.prompt = "总共花费"
- self.zhongjianzhi = ""
- count = 0
- for index in range(5, -1, -1):
- if x[index] > 0:
- count += 1
- self.zhongjianzhi = str(str(x[index]) + self.danwei[index])+ self.zhongjianzhi
- # print("我是中间值,我的index = %d,count = %d , " % (index,count) +self.zhongjianzhi)
- elif x[index] < 0 :
- x[index - 1] -= 1
- x[index] += self.zhuanhua[index]
- # print("我把X给转化啦!", x)
- return self.zhuanhuan(x)
-
- self.prompt += self.zhongjianzhi
- # print("我是prompt,我是%s index = %d count = %d " % (self.prompt,index,count))
复制代码
自己写出来了,哈哈,最后的zhuanhuan函数就是将-的值进行转化成正数,然后输出
会的人可以不予理会 |
|