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')
