这里写了一个计时器实例,来自Python课堂视频学习的代码
#coding=utf-8
import time as t
class Mytimer():
def __init__(self):
self.unit = ["年","月", "日", "小时", "分钟", "秒"]
self.prompt = "未开始计时!"
self.lasted = []
self.begin = 0
self.end = 0
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 st

这篇博客分享了如何在Python中实现一个简单的计时器功能,通过代码实例展示了具体的实现过程,适用于初学者入门实践。
最低0.47元/天 解锁文章

483

被折叠的 条评论
为什么被折叠?



