鱼C论坛

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

[学习笔记] Python44讲课后题进阶版时间

[复制链接]
发表于 2019-4-24 22:27:18 | 显示全部楼层 |阅读模式

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

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

x

  1. import time as t

  2. class Time():
  3.     def __init__(self):
  4.         self.unit = ['年','月','日','时','分','秒']
  5.         self.prompt = "未开始计时!"
  6.         self.lasted = []
  7.         self.begin = 0
  8.         self.end = 0
  9.         self.borrow = [0,12,0,24,60,60]
  10.         
  11.         
  12.     def __str__(self):
  13.         return self.prompt

  14.     __repr__ = __str__


  15.     #时间相加计算
  16.     def __add__(self,other):
  17.         prompt = '总共运行了'
  18.         result = []
  19.         for i in range(6):
  20.             result.append(self.lasted[i] + other.lasted[i])
  21.             if result[i]:
  22.                 prompt += (str(result[i]) + self.unit[i])
  23.         return prompt
  24.         
  25.    
  26.     #开始计时
  27.     def start(self):
  28.         self.begin = t.localtime()
  29.         self.prompt = '提示:请先调用stop()停止计时!'
  30.         print('计时开始……')
  31.         prompt = '开始时间为:'
  32.         for i in range(6):
  33.             if self.begin[i]:
  34.                 prompt += (str(self.begin[i]) + self.unit[i])
  35.   
  36.         #时间借位时,借位列表
  37.         temp = self.begin [1]
  38.         if (temp == 1) or (temp == 3) or (temp == 5) or(temp == 7) or (temp == 8) or (temp == 10) or(temp == 12):
  39.             self.borrow[2] = 31
  40.         else:
  41.             if (temp == 2):
  42.                 self.borrow[2] = 29
  43.             else:
  44.                 self.borrow[2] = 30
  45.         return prompt

  46.     #停止计时
  47.     def stop(self):
  48.         if not self.begin:
  49.             print('提示;请先调用start()进行计时')
  50.         else:
  51.             self.end = t.localtime()
  52.             self._cale()
  53.             prompt = '结束时间为:'
  54.             for i in range(6):
  55.                 if self.end[i]:
  56.                     prompt += (str(self.end[i]) + self.unit[i])
  57.             self._initialize()
  58.             print('计时结束!')
  59.             return prompt
  60.             
  61.             
  62.         
  63.     #计算运行时间
  64.     def _cale(self):
  65.         self.lasted = []
  66.         self.prompt = "总共运行了"
  67.         for i in range(6):
  68.             self.lasted.append(self.end[i] - self.begin[i])
  69.             
  70.         #借位操作,计算时间
  71.       
  72.         for i in [5,4,3,2,1,0]:
  73.             temp = self.lasted[i]
  74.             if temp <0:
  75.                 j = 1
  76.                 while self.lasted[i - j] <1:
  77.                     self.lasted[i - j] += self.borrow[i - j]-1
  78.                     self.lasted[i - j-1] -= 1
  79.                     j += 1
  80.                 self.lasted[i] += self.borrow[i]
  81.                 self.lasted[i -1] -= 1

  82.         for i in range(6):
  83.             if self.lasted[i]:
  84.                 self.prompt += (str(self.lasted[i]) + self.unit[i])
  85.          

  86. #为下一轮计时初始化
  87.     def _initialize(self):
  88.         self.begin = 0
  89.         self.end = 0  
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-17 07:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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