第044讲 课后练习 答案好像有问题
本帖最后由 heidern0612 于 2020-6-14 18:59 编辑在第044讲的课后练习当中, 0 的答案好像有bug
import time as t
class MyTimer:
def __init__(self):
self.unit = ['年', '月', '天', '小时', '分钟', '秒']
self.borrow =
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 + other.lasted)
if result:
prompt += (str(result) + self.unit)
return prompt
# 开始计时
def start(self):
self.begin = t.localtime()
self.prompt = "提示:请先调用 stop() 停止计时!"
print("计时开始...")
# 停止计时
def stop(self):
if not self.begin:
print("提示:请先调用 start() 进行计时!")
else:
self.end = t.localtime()
self._calc()
print("计时结束!")
# 内部方法,计算运行时间
def _calc(self):
self.lasted = []
self.prompt = "总共运行了"
for index in range(6):
temp = self.end - self.begin
# 低位不够减,需向高位借位
if temp < 0:
# 测试高位是否有得“借”,没得借的话向再高位借......
i = 1
while self.lasted < 1:
self.lasted += self.borrow - 1
self.lasted -= 1
i += 1
self.lasted.append(self.borrow + temp)
self.lasted -= 1
else:
self.lasted.append(temp)
# 由于高位随时会被借位,所以打印要放在最后
for index in range(6):
if self.lasted:
self.prompt += str(self.lasted) + self.unit
# 为下一轮计时初始化变量
self.begin = 0
self.end = 0
第53行的 self.lasted += self.borrow - 1
不应该-1
比如开始时间是2020/6/14 0:0:59
结束时间是2020/6/14 1:0:1
按照该程序算出的结果会是58分2秒
实际上是59分2秒 ……还得数一下,用代码格式发会更好^_^
import time as t
class MyTimer:
def __init__(self):
self.unit = ['年', '月', '天', '小时', '分钟', '秒']
self.borrow =
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 + other.lasted)
if result:
prompt += (str(result) + self.unit)
return prompt
# 开始计时
def start(self):
self.begin = t.localtime()
self.prompt = "提示:请先调用 stop() 停止计时!"
print("计时开始...")
# 停止计时
def stop(self):
if not self.begin:
print("提示:请先调用 start() 进行计时!")
else:
self.end = t.localtime()
self._calc()
print("计时结束!")
# 内部方法,计算运行时间
def _calc(self):
self.lasted = []
self.prompt = "总共运行了"
for index in range(6):
temp = self.end - self.begin
# 低位不够减,需向高位借位
if temp < 0:
# 测试高位是否有得“借”,没得借的话向再高位借......
i = 1
while self.lasted < 1:
self.lasted += self.borrow - 1
self.lasted -= 1
i += 1
self.lasted.append(self.borrow + temp)
self.lasted -= 1
else:
self.lasted.append(temp)
# 由于高位随时会被借位,所以打印要放在最后
for index in range(6):
if self.lasted:
self.prompt += str(self.lasted) + self.unit
# 为下一轮计时初始化变量
self.begin = 0
self.end = 0 试试这个~
import time as t
class MyTimer:
# 初始化变量
def __init__(self):
self.prompt = '未开始计时!'
self.lasted = []
self.unit = ['年','月','日','时','分','秒']
self.ary =
self.begin = 0
self.end = 0
# 打印结果
def __str__(self):
return self.prompt
__repr__ = __str__
# 计算运行总时间
def __add__(self, other):
prompt = '合计运行了:'
lasted = []
for index in range(6):
lasted.append(self.lasted + other.lasted)
if lasted:
prompt += (str(lasted) + self.unit)
return prompt
# 开始计时
def start(self):
self.lasted = []
self.begin = t.localtime()
self.prompt = '提示:请先调用stop函数停止计时!'
print('计时开始!')
# 结束计时
def stop(self):
if not self.begin:
print('提示:请先调用start函数开始计时!')
else:
self.end = t.localtime()
self.__calc()
print('计时结束!')
# 计算运行时间
def __calc(self):
self.prompt = '总共运行时间为'
for index in range(6):
judge = self.end - self.begin
if judge < 0 :
new_lasted = self.ary + judge
self.lasted.append(new_lasted)
if index >= 1 :
self.lasted.insert(index-1,self.lasted-1)
self.lasted.pop(index)
else:
self.lasted.append(judge)
for index in range(6):
if self.lasted:
self.prompt += (str(self.lasted) + self.unit)
# 初始化变量,便于下一次的计时
self.begin = 0
self.end = 0
liuzhengyuan 发表于 2020-6-14 18:44
……还得数一下,用代码格式发会更好^_^
额,帮我判断一下我说的对不对呗 tanzy2 发表于 2020-6-14 18:54
额,帮我判断一下我说的对不对呗
好像是对的,那不应该 -1
我刚刚用电脑修改时间测试了下,,确实有误差
但是不必纠结于此,甲鱼老师只是想让我们练习~~~而且他也说了
实际上我们做的还是把很多因素都没考虑进去,实际上误差还会更大,找的出错误就很强了~
Twilight6 发表于 2020-6-14 23:21
好像是对的,那不应该 -1
我刚刚用电脑修改时间测试了下,,确实有误差
好的,我的强迫症好了,哈哈哈{:10_277:} tanzy2 发表于 2020-6-15 20:33
好的,我的强迫症好了,哈哈哈
实际测试时候 57多...不知道是不是我操作失误了
页:
[1]