鱼C论坛

 找回密码
 立即注册
查看: 91|回复: 7

[已解决]python44讲

[复制链接]
发表于 2025-4-25 16:10:29 | 显示全部楼层 |阅读模式

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

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

x
import time as t
class MyTimer:
    def __str__(self):
        return self.prompt
    __repr__ = __str__
    #开始计时
    def start(self):
        self.begin = t.localtime()
        print('开始计时...')
    #停止计时
    def stop(self):
        self.end = t.localtime()
        self._calc()
        print('停止计时')
    #内部方法,计算运行时间
    def _calc(self):
        self.lasted = []
        self.prompt = '总共运行'
        for index in range(6):
            self.lasted.append(self.end[index] - self.begin[index])
            self.prompt += str(self.lasted[index])
问题:不显示运行时间
最佳答案
2025-4-25 21:44:57
本帖最后由 jackz007 于 2025-4-25 22:39 编辑

        代码没有问题,只需要加上对象定义、方法调用、信息显示等代码即可看到效果
  1. # 以下是楼主的代码

  2. import time as t
  3. class MyTimer:
  4.     def __str__(self):
  5.         return self.prompt
  6.     __repr__ = __str__
  7.     #开始计时
  8.     def start(self):
  9.         self.begin = t.localtime()
  10.         print('开始计时...')
  11.     #停止计时
  12.     def stop(self):
  13.         self.end = t.localtime()
  14.         self._calc()
  15.         print('停止计时')
  16.     #内部方法,计算运行时间
  17.     def _calc(self):
  18.         self.lasted = []
  19.         self.prompt = '总共运行'
  20.         for index in range(6):
  21.             self.lasted.append(self.end[index] - self.begin[index])
  22.             self.prompt += str(self.lasted[index])

  23. # 以下是新添加的代码

  24. r = MyTimer()
  25. r . start()
  26. t . sleep(10)
  27. r . stop()
  28. print(r . prompt)
复制代码


        把上面的代码保存为文件 "mt.py"

        【运行实况】:

  1. D:\[00.Exercise]\[Python]>python mt.py
  2. 开始计时...
  3. 停止计时
  4. 总共运行0000010

  5. D:\[00.Exercise]\[Python]>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-4-25 16:11:55 | 显示全部楼层
解答:代码问题与修正方案

你的代码存在三个主要问题:

  • 问题1:使用 t.localtime() 记录时间(返回结构化时间对象,无法直接相减)
  • 问题2self.lasted = 未初始化且未正确计算时间差
  • 问题3self.prompt 字符串拼接逻辑错误


修正后的完整代码


  1. import time as t

  2. class MyTimer:
  3.     def __str__(self):
  4.         return self.prompt
  5.     __repr__ = __str__
  6.    
  7.     def start(self):
  8.         self.begin = t.time()  # 改用时间戳
  9.         print('开始计时...')
  10.    
  11.     def stop(self):
  12.         self.end = t.time()    # 改用时间戳
  13.         self._calc()
  14.         print('停止计时')
  15.    
  16.     def _calc(self):
  17.         # 计算时间差(秒为单位)
  18.         lasted_seconds = self.end - self.begin
  19.         
  20.         # 转换为时分秒格式
  21.         hours = lasted_seconds // 3600
  22.         minutes = (lasted_seconds % 3600) // 60
  23.         seconds = lasted_seconds % 60
  24.         
  25.         # 生成易读提示
  26.         self.prompt = f"总共运行了{int(hours)}小时{int(minutes)}分{seconds:.2f}秒"
复制代码


改进说明

  • 改用 t.time() 记录时间戳,支持直接相减计算差值
  • 通过 divmod 计算时分秒,避免直接操作 struct_time 的复杂性
  • 使用 f-string 格式化输出结果,代码更简洁易读


(调用示例:timer = MyTimer() → timer.start() → 执行被测代码 → timer.stop() → print(timer))

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-4-25 17:23:24 | 显示全部楼层
本帖最后由 player-none 于 2025-4-25 17:25 编辑
  1. import time as t
  2. class MyTimer:
  3.     #上面两个方法没有必要
  4.     #开始计时
  5.     def start(self):
  6.         self.begin = t.time()
  7.         print('开始计时...')
  8.     #停止计时
  9.     def stop(self):
  10.         self.end = t.time()
  11.         self._calc()
  12.         print('停止计时')
  13.     #内部方法,计算运行时间
  14.     def _calc(self):
  15.         prompt = f'总共运行{self.end - self.start}秒'
  16.         print(prompt)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-4-25 17:34:16 | 显示全部楼层

我跟着视频打的代码,他是有问题吗,还是什么其他原因
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-4-25 17:40:24 | 显示全部楼层
追逐我的明天 发表于 2025-4-25 17:34
我跟着视频打的代码,他是有问题吗,还是什么其他原因

估计是你应该在执行完所有命令之后加一行print(变量名),你忘加了(time.localtime用法我不太清楚)

我的代码能正确执行吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-4-25 19:35:51 | 显示全部楼层
player-none 发表于 2025-4-25 17:40
估计是你应该在执行完所有命令之后加一行print(变量名),你忘加了(time.localtime用法我不太清楚)

...


                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-4-25 21:06:59 | 显示全部楼层

图片一直在转,显示不出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-4-25 21:44:57 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2025-4-25 22:39 编辑

        代码没有问题,只需要加上对象定义、方法调用、信息显示等代码即可看到效果
  1. # 以下是楼主的代码

  2. import time as t
  3. class MyTimer:
  4.     def __str__(self):
  5.         return self.prompt
  6.     __repr__ = __str__
  7.     #开始计时
  8.     def start(self):
  9.         self.begin = t.localtime()
  10.         print('开始计时...')
  11.     #停止计时
  12.     def stop(self):
  13.         self.end = t.localtime()
  14.         self._calc()
  15.         print('停止计时')
  16.     #内部方法,计算运行时间
  17.     def _calc(self):
  18.         self.lasted = []
  19.         self.prompt = '总共运行'
  20.         for index in range(6):
  21.             self.lasted.append(self.end[index] - self.begin[index])
  22.             self.prompt += str(self.lasted[index])

  23. # 以下是新添加的代码

  24. r = MyTimer()
  25. r . start()
  26. t . sleep(10)
  27. r . stop()
  28. print(r . prompt)
复制代码


        把上面的代码保存为文件 "mt.py"

        【运行实况】:

  1. D:\[00.Exercise]\[Python]>python mt.py
  2. 开始计时...
  3. 停止计时
  4. 总共运行0000010

  5. D:\[00.Exercise]\[Python]>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 12:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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