from time import sleep import time import os class Clock(object): """数字时钟""" def __init__(self, hour=0, minute=0, second=0): """初始化方法 :param hour: 时 :param minute: 分 :param second: 秒 """ self._hour = hour self._minute = minute self._second = second def run(self): """走字""" self._second += 1 if self._second == 60: self._second = 0 self._minute += 1 if self._minute == 60: self._minute = 0 self._hour += 1 if self._hour == 24: self._hour = 0 def show(self): """显示时间""" return '%02d:%02d:%02d' % \ (self._hour, self._minute, self._second) def main(): localtime = time.asctime( time.localtime(time.time()) ) hour = int(localtime[11:13]) min = int(localtime[14:16]) sec = int(localtime[17:19]) clock = Clock(hour, min, sec) while True: print(clock.show()) sleep(1) clock.run() os.system('cls') if __name__ == '__main__': main()
获取系统时间,
import time localtime = time.asctime( time.localtime(time.time()) )
python中强制转换int型
hour = int(localtime[11:13])
python中的字符串截取
str = ‘0123456789’ print str[0:3] #截取第一位到第三位的字符 print str[:] #截取字符串的全部字符 print str[6:] #截取第七个字符到结尾 print str[:-3] #截取从头开始到倒数第三个字符之前 print str[2] #截取第三个字符 print str[-1] #截取倒数第一个字符 print str[::-1] #创造一个与原字符串顺序相反的字符串 print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符 print str[-3:] #截取倒数第三位到结尾 print str[:-5:-3] #逆序截取,具体啥意思没搞明白?
python清空终端
import os os.system('cls')