鱼C论坛

 找回密码
 立即注册
查看: 1243|回复: 1

[已解决]第44课有一个地方没有搞明白

[复制链接]
发表于 2017-9-7 20:25:58 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 LYqiang 于 2017-9-7 20:34 编辑

在第44课的中简单的定制计时器上课代码:
  1. import time as t

  2. class MyTimer():
  3.    
  4.     def __init__(self):
  5.         self.unit = ['年','月','日','小时','分钟','秒']
  6.         self.prompt = '未开始计时'
  7.         self.lasted = []
  8.         self.begin = 0
  9.         self.end = 0
  10.         
  11.     def __str__(self):
  12.         return self.prompt
  13.    

  14.     __repr__ = __str__   
  15.    
  16.     #开始计时
  17.     def start(self):
  18.         self.begin = t.localtime()
  19.         self.prompt = '提示:请先调用stop()停止计时!'
  20.         print('计时开始')
  21.         
  22.     #停止计时
  23.     def stop(self):
  24.         self.end = t.localtime()
  25.         if not self.begin:
  26.             print('提示:请先调用start()进行计时!')
  27.         self.__calc()
  28.         print('计时结束')
  29.         
  30.     #内部方法,计算运行时间
  31.     def __calc(self):
  32.         self.lasted = []
  33.         self.prompt = '总共运行了'
  34.         for index in range(6):
  35.             self.lasted.append(self.end[index] - self.begin[index])
  36.             if self.lasted[index]:
  37.                 self.prompt += (str(self.lasted[index] + self.unit[index])
  38.         #为下一轮计时初始化变量
  39.         self.begin = 0
  40.         self.end = 0
复制代码


        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
         
      这个部分,为什么self.end和self.begin后面需要加上[index]下标

index索引范围0-5的值又是什么,怎么通过for循环的方式实现打印index 0-5的值,求大神指点一下,
最佳答案
2017-9-7 20:37:53
end得到是一个元组。前6项分别是年月日时分秒
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=7, tm_hour=20, tm_min=36, tm_sec=30, tm_wday=3, tm_yday=250, tm_isdst=0)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-7 20:37:53 | 显示全部楼层    本楼为最佳答案   
end得到是一个元组。前6项分别是年月日时分秒
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=9, tm_mday=7, tm_hour=20, tm_min=36, tm_sec=30, tm_wday=3, tm_yday=250, tm_isdst=0)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-2 05:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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