python 计时器

#! /usr/bin/env python
#! -*- cording:utf-8 -*-

import  time as t

class Mytime():
    def __init__(self):
        self.unit=['','','',"小时",'分钟','']
        self.prompt="未开始计时!"

        self.begin=0
        self.end=0
#属性和方法不能同名,属性会覆盖方法
    def start(self):
        self.begin =t.localtime()
        print("计时开始"+str(self.begin))
    def stop(self):
        self.pause=input("请输入0")
        self.int_pause=int(self.pause)#要转换输入的类型
        if self.int_pause ==0:
            self.end=t.localtime()
            self.__calc()
            print("计时结束")
     #内部方法,计算运行时间
    def __calc(self):
        self.lasted=[]#声明一个列表
        self.prompt="总共运行了"#赋值就是定义
        for index in range(6):#localtime的六个元素
            self.lasted.append(self.end[index]-self.begin[index])
            if self.lasted[index]:
                self.prompt +=(str(self.lasted[index])+self.unit[index])#取最后一个秒数,并加上单位
        print(self.prompt)
t1=Mytime()
t1.start()

t1.stop()
原文地址:https://www.cnblogs.com/aqiuarcadia/p/7392252.html