鱼C论坛

 找回密码
 立即注册
查看: 3940|回复: 4

[已解决]关于小甲鱼学python044课后题的第0题的一些疑问

[复制链接]
发表于 2017-11-22 15:47:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本来准备上传图片的,但是限制3个,然后中途退出了,既然就已经达到上传上限了。希望论坛可以改进一下,在还没有发表之前,怎么也不能算用过了吧。

begin = (2017, 11, 22, 14, 0, 47,)
end = (2017, 11, 22, 15, 0, 41,)
lasted = []
borrow = [0,12,31,24,60,60]

for index in range(6):
    temp = end[index] - begin[index]
    if temp < 0:
         i = 1
         while lasted[index-i] < 1:
             lasted[index-i] += borrow[index-i] -1
             lasted[index-i-1] -= 1
             i += 1

         lasted.append(borrow[index] + temp)
         lasted[index-1] -= 1
    else:
        lasted.append(temp)
    print(lasted[index])
print(lasted)

上面的代码得到的结果是
0, 0, 0, 1, 0, 54
[-2, 10, 29, 23, 57, 54]
为何索引出来的结果和打印列表出来的结果不同呢


当我在 if 语句里只保留 lasted.append(borrow[index] + temp) 时:
begin = (2017, 11, 22, 14, 0, 47,)
end = (2017, 11, 22, 15, 0, 41,)
lasted = []
borrow = [0,12,31,24,60,60]

for index in range(6):
    temp = end[index] - begin[index]
    if temp < 0:
         # i = 1
         # while lasted[index-i] < 1:
         #     lasted[index-i] += borrow[index-i] -1
         #     lasted[index-i-1] -= 1
         #     i += 1
         #
          lasted.append(borrow[index] + temp)
         # lasted[index-1] -= 1
    else:
        lasted.append(temp)
    print(lasted[index])
print(lasted)

得到的结果如下:
0, 0, 0, 1, 0, 54
[0, 0, 0, 1, 0, 54]
那么这段代码到底有什么用呢,不用 while 语句里的代码也可以得到同样的结果,而且在直接打印列表与 索引的结果也是一致的。
最佳答案
2017-12-7 16:29:07
重新调试了一下,发现运行你提供的参数,还是需要执行while语句,不过执行逻辑和甲鱼老师的不一样。
最大的改变是把下列两条语句移到for循环的开始,然后再执行while语句:
  1. lasted.append(borrow[index] + temp)
  2. lasted[index-1] -= 1
复制代码

其中,while语句逻辑有少量改动,详见我的帖子:
http://bbs.fishc.com/thread-101090-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-11-22 16:40:05 | 显示全部楼层
@小甲鱼
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-7 15:27:03 | 显示全部楼层
我也刚学到这里。按照读取语句来分析:
  1.          i = 1
  2.          while lasted[index-i] < 1:
  3.              lasted[index-i] += borrow[index-i] -1
  4.              lasted[index-i-1] -= 1
  5.              i += 1
复制代码

这段代码根本就没有机会进行读取。
因为时间是从年月日时分秒依次读取的。只要不是乱设置,结束年份不可能小于开始年份。所以lasted[0]永远大于等于0。
后面的语句,一旦temp<0,会执行高位减一,本位加借位的操作。
  1.          lasted.append(borrow[index] + temp)   ##本位加借位列表对应的数
  2.          lasted[index-1] -= 1                  ##高位减一
复制代码

操作后,存进lasted的数均>0,不会触发lasted[index-i] < 1的情况。

例如:(这里假设1个月都有31天)
结束时间为: (2017, 5, 15, 20, 30, 40)
开始时间为: (2016, 10, 20, 22, 40, 50)
利用算术直接相减的结果为:(1,-5,-5,-2,-10,-10)  ##例子中的-5会经过处理后再放入列表,不存在连续两位为负数的情况。
而我们读得懂的期望结果为(0,6,25,9,49,50)。

目前# lasted[index-1] -= 1,结果为[1, 7, 26, 22, 50, 50],刚好少了高位借位的步骤。把这个语句加上再试试吧。

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-7 16:29:07 | 显示全部楼层    本楼为最佳答案   
重新调试了一下,发现运行你提供的参数,还是需要执行while语句,不过执行逻辑和甲鱼老师的不一样。
最大的改变是把下列两条语句移到for循环的开始,然后再执行while语句:
  1. lasted.append(borrow[index] + temp)
  2. lasted[index-1] -= 1
复制代码

其中,while语句逻辑有少量改动,详见我的帖子:
http://bbs.fishc.com/thread-101090-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-8 16:38:55 | 显示全部楼层
payton24 发表于 2017-12-7 16:29
重新调试了一下,发现运行你提供的参数,还是需要执行while语句,不过执行逻辑和甲鱼老师的不一样。
最大 ...

3Q,我试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-19 13:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表