第44講:簡單定製
阿新 • • 發佈:2020-09-04
一 基本要求
- 定製一個計時器的類
- start和stop方法代表啟動計時和停止計時
- 假設計時器物件t1,print(t1)和直接呼叫t1均顯示結果
- 當計時器未啟動或已經停止計時時,呼叫stop方法會給予溫馨提示
- 兩個計時器物件可以進行相加:t1+t2
- 只能使用提供的有限資源完成
二 需要的資源
- 使用time模組的localtime方法獲取時間
- time.localtime返回struct_time的時間格式
- 表現你的類:__str__和__repr__
三 基本程式碼
class43_MyTimer.py檔案
1 import time as t 2 3 class MyTimer(): 4 def __init__(self): 5 self.unit = ['年','月','日','小時','分鐘','秒'] 6 self.prompt = "未開始計時!" 7 self.lasted = [] 8 self.begin = 0 9self.end = 0 10 11 def __str__(self): 12 return self.prompt 13 14 __repr__ = __str__ 15 16 def __add__(self,other): 17 prompt = "總共運行了" # 臨時使用,方法內部區域性變數 18 result = [] 19 for index in range(6): 20 result.append(self.lasted[index] + other.lasted[index])21 if result[index]: 22 prompt += (str(result[index]) + self.unit[index]) 23 return prompt 24 25 # 開始計時 26 def start(self): 27 self.begin = t.localtime() 28 self.prompt = "提示:請先呼叫stop()停止計時!" 29 print("計時開始...") 30 31 # 停止計時 32 def stop(self): 33 if not self.begin: 34 print("提示:請先呼叫start()進行計時!") 35 else: 36 self.end = t.localtime() 37 self._calc() 38 print("計時結束!") 39 40 # 內部方法,計算執行時間 41 def _calc(self): 42 self.lasted = [] 43 self.prompt = "總共運行了" 44 for index in range(6): 45 self.lasted.append(self.end[index]-self.begin[index]) 46 if self.lasted[index]: 47 self.prompt += (str(self.lasted[index]) + self.unit[index]) 48 49 # 為下一輪計時初始化變數 50 self.begin = 0 51 self.end = 0
執行結果:
1 >>> import class43_MyTimer as T 2 >>> t1 = T.MyTimer() 3 >>> t1.start() 4 計時開始... 5 >>> t1.stop() 6 總共運行了6秒 7 計時結束! 8 >>> t2 = T.MyTimer() 9 >>> t2 10 未開始計時! 11 >>> t2.stop() 12 提示:請先呼叫start()進行計時! 13 >>> t2.start() 14 計時開始... 15 >>> t2 16 提示:請先呼叫stop()停止計時! 17 >>> t2.stop() 18 總共運行了1分鐘-44秒 19 計時結束! 20 >>> t1 + t2 21 '總共運行了1分鐘-38秒' 22 >>>
四 課後習題
0. 按照課堂中的程式,如果開始計時的時間是(2022年2月22日16:30:30),停止時間是(2025年1月23日15:30:30),那按照我們用停止時間減開始時間的計算方式就會出現負數,你應該對此做一些轉換。
1 import time as t 2 3 class MyTimer(): 4 def __init__(self): 5 self.unit = ['年','月','日','小時','分鐘','秒'] 6 self.borrow = [0,12,31,24,60,60] 7 self.prompt = "未開始計時!" 8 self.lasted = [] 9 self.begin = 0 10 self.end = 0 11 12 def __str__(self): 13 return self.prompt 14 15 __repr__ = __str__ 16 17 def __add__(self,other): 18 prompt = "總共運行了" # 臨時使用,方法內部區域性變數 19 result = [] 20 for index in range(6): 21 result.append(self.lasted[index] + other.lasted[index]) 22 if result[index]: 23 prompt += (str(result[index]) + self.unit[index]) 24 return prompt 25 26 # 開始計時 27 def start(self): 28 self.begin = t.localtime() 29 self.prompt = "提示:請先呼叫stop()停止計時!" 30 print("計時開始...") 31 32 # 停止計時 33 def stop(self): 34 if not self.begin: 35 print("提示:請先呼叫start()進行計時!") 36 else: 37 self.end = t.localtime() 38 self._calc() 39 print("計時結束!") 40 41 # 內部方法,計算執行時間 42 def _calc(self): 43 self.lasted = [] 44 self.prompt = "總共運行了" 45 for index in range(6): 46 temp = self.end[index]-self.begin[index] 47 48 # 低位不夠減,需要向高位借位 49 if temp < 0: 50 # 測試高位是否有得借,沒得借的話再向高位借...... 51 i = 1 52 while self.lasted[index-i] < 1: 53 self.lasted[index-i] += self.borrow[index-i] - 1 54 self.lasted[index-i-1] -= 1 55 i += 1 56 57 self.lasted.append(self.borrow[index] + temp) 58 self.lasted[index-1] -= 1 59 else: 60 self.lasted.append(temp) 61 62 # 由於高位隨時會被借位,所以列印要放在最後 63 for index in range(6): 64 if self.lasted[index]: 65 self.prompt += str(self.lasted[index]) + self.unit[index] 66 67 print(self.prompt) 68 # 為下一輪計時初始化變數 69 self.begin = 0 70 self.end = 0