|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import time as t
class My:
def __str__(self):
return self.prompt
__repr__ = __str__
def __init__(self):
self.unit = ['年','月','日','小时','分钟','秒']
self.prompt = '未开始计时'
self.lasted = []
self.begin = 0
self.end = 0
#开始计时
def start(self):
self.start = t.localtime()
print('计时开始!')
#结束计时
def stop(self):
self.stop = t.localtime()
self._calc()
print('计时结束!')
#内部方法,计算时间
def _calc(self):
self.lasted = []
self.prompt = '总共运行了'
for index in range(6):
if self.stop[index] >= self.start[index]:
return self.lasted.append(self.stop[index] - self.start[index])
else:
return slef.lasted.append(self.start[index] - self.stop[index])
self.prompt += str(self.lasted[index]) + str(self.unit[index])
print(self.prompt)
我向创建一个计时器,然后在小甲鱼的代码的基础上进行改动,要求如果开始的时候为2021年,结束的时候为2025年,两个相减不会出现负数,不知道这个代码问题出在了哪里,运行起来是这样的
>>> a = My()
>>> a.start()
计时开始!
>>> a.stop()
计时结束!
>>> a
总共运行了
- import time
- start = time.time()
- time.sleep(2)
- end = time.time()
- print(f"一共过了 {int(end-start)} 秒")
复制代码
|
|