鱼C论坛

 找回密码
 立即注册
查看: 2051|回复: 0

关于第44讲 将负时间转化

[复制链接]
发表于 2016-4-1 16:13:15 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 林斌 于 2016-4-1 18:24 编辑
  1. import time as t

  2. class MyTimer:
  3.         # 初始化
  4.         def __init__(self):
  5.                 self.begin = 0
  6.                 self.end = 0
  7.                 self.prompt = "啥都没干,让我显示什么,赶紧输入 **.start()"
  8.                 self.danwei = ["年", "月", "日", "时", "分", "秒"]
  9.                 self.liangzhe = []
  10.                 self.zhuanhua = ['', 12, 30, 24, 60, 60]

  11.         # 打印 显示
  12.         def __str__(self):
  13.                 return self.prompt

  14.         #两者相加
  15.         def __add__(self, other):
  16.                 self.prompt = "总共花费"
  17.                 for i in range(6):
  18.                         self.liangzhe.append(self.lasted[i] + other.lasted[i])
  19.                 self.zhuanhuan(self.liangzhe)
  20.                 # print(self.liangzhe)
  21.                 return self.prompt

  22.         __repr__ = __str__
  23.         # 开始计时
  24.         def start(self):
  25.                 self.begin = t.localtime()
  26.                 print("开始计时")

  27.         # 计时结束
  28.         def stop(self):
  29.                 if not self.begin:
  30.                         print("提示:请先调用 start()开始计时!")
  31.                 else:
  32.                         self.end = t.localtime()
  33.                         self._jisuan()
  34.                         print("计时结束")

  35.         # 定义内部方法,进行时间计算
  36.         def _jisuan(self):
  37.                 self.lasted = []
  38.                 self.prompt = "总共花费"
  39.                 for index in range(6):
  40.                         self.lasted.append(self.end[index] - self.begin[index])
  41.                 # print(self.lasted)
  42.                 self.zhuanhuan(self.lasted)

  43.         # 定义一个转换函数(将时间清晰化)
  44.         
  45.         def zhuanhuan(self, x):
  46.                 # print("执行转换函数了!!")
  47.                 # print(x)
  48.                 self.prompt = "总共花费"
  49.                 self.zhongjianzhi = ""
  50.                 count = 0
  51.                 for index in range(5, -1, -1):
  52.                         if x[index] > 0:
  53.                                count += 1
  54.                                self.zhongjianzhi = str(str(x[index]) + self.danwei[index])+ self.zhongjianzhi
  55.                                # print("我是中间值,我的index = %d,count = %d , " % (index,count) +self.zhongjianzhi)
  56.                         elif x[index] < 0 :
  57.                                 x[index - 1] -= 1
  58.                                 x[index] += self.zhuanhua[index]
  59.                                 # print("我把X给转化啦!", x)
  60.                                 return self.zhuanhuan(x)
  61.                                 
  62.                 self.prompt += self.zhongjianzhi
  63.                 # print("我是prompt,我是%s  index = %d count = %d  " % (self.prompt,index,count))
复制代码

自己写出来了,哈哈,最后的zhuanhuan函数就是将-的值进行转化成正数,然后输出

会的人可以不予理会
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-20 11:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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