鱼C论坛

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

[已解决]请问下为什么运行结果没有显示秒数啊???

[复制链接]
发表于 2020-8-19 15:44:00 | 显示全部楼层 |阅读模式

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

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

x
计时器代码:
import time as t

class MyTimer():
    def __init__(self):
        self.unit = ["年","月","天","小时","分钟","秒"]
        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[index] + other.lasted[index])
            if result[index]:
                prompt += (str(result[index]) + self.unit[index])
        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):
            self.lasted.append(self.end[index] - self.begin[index])
            if self.lasted[index]:
                self.prompt += (str(self.lasted[index]) + self.unit[index])
        # 为下一轮计时初始化变量
        self.begin = 0
        self.end = 0


t1 = MyTimer()
t1.start()
t1.stop()
print(t1)


结果:
计时开始...
计时结束!
总共运行了

最佳答案
2020-8-19 16:05:52
学抓蟒蛇 发表于 2020-8-19 15:57
这样确实可以,但是可不可以不加强制等待也显示秒数呀?



你分开执行就行的

555.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-19 15:50:11 | 显示全部楼层
本帖最后由 Twilight6 于 2020-8-19 15:51 编辑



将你的代码分开执行,你这直接运行太快了,分开执行就可以了

先运行:
  1. t1 = MyTimer()
  2. t1.start()
复制代码


然后去控制台时候在运行:
  1. t1.stop()
  2. print(t1)
复制代码

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

使用道具 举报

发表于 2020-8-19 15:51:11 | 显示全部楼层
因为程序运行的时间不足 1 秒,所以不会显示,加上 sleep(1)(表示让程序暂停一秒)就能显示了

  1. import time as t

  2. class MyTimer():
  3.     def __init__(self):
  4.         self.unit = ["年","月","天","小时","分钟","秒"]
  5.         self.prompt = "未开始计时!"
  6.         self.lasted = []
  7.         self.begin = 0
  8.         self.end = 0

  9.     def __str__(self):
  10.         return self.prompt

  11.     __repr__ = __str__

  12.     def __add__(self, other):
  13.         prompt = "总共运行了"
  14.         result = []
  15.         for index in range(6):
  16.             result.append(self.lasted[index] + other.lasted[index])
  17.             if result[index]:
  18.                 prompt += (str(result[index]) + self.unit[index])
  19.         return prompt

  20.     # 开始计时
  21.     def start(self):
  22.         self.begin = t.localtime()
  23.         self.prompt = "提示:请先调用stop()停止计时!"
  24.         print("计时开始...")

  25.     # 停止计时
  26.     def stop(self):
  27.         if not self.begin:
  28.             print("提示:请先调用start()进行计时!")
  29.         else:
  30.             self.end = t.localtime()
  31.             self._calc()
  32.             print("计时结束!")

  33.     # 内部方法,计算运行时间
  34.     def _calc(self):
  35.         self.lasted = []
  36.         self.prompt = "总共运行了"
  37.         for index in range(6):
  38.             self.lasted.append(self.end[index] - self.begin[index])
  39.             if self.lasted[index]:
  40.                 self.prompt += (str(self.lasted[index]) + self.unit[index])
  41.         # 为下一轮计时初始化变量
  42.         self.begin = 0
  43.         self.end = 0


  44. t1 = MyTimer()
  45. t1.start()
  46. t.sleep(1)
  47. t1.stop()
  48. print(t1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-8-19 15:56:13 | 显示全部楼层
Twilight6 发表于 2020-8-19 15:50
将你的代码分开执行,你这直接运行太快了,分开执行就可以了

先运行:

分开执行也显示不了呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-19 15:57:21 | 显示全部楼层
zltzlt 发表于 2020-8-19 15:51
因为程序运行的时间不足 1 秒,所以不会显示,加上 sleep(1)(表示让程序暂停一秒)就能显示了

这样确实可以,但是可不可以不加强制等待也显示秒数呀?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-19 16:05:52 | 显示全部楼层    本楼为最佳答案   
学抓蟒蛇 发表于 2020-8-19 15:57
这样确实可以,但是可不可以不加强制等待也显示秒数呀?



你分开执行就行的

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

使用道具 举报

 楼主| 发表于 2020-8-19 16:06:08 | 显示全部楼层
原来在Python控制台执行代码才能显示秒数,直接执行代码的只能像鱼友说的,加sleep()才能显示秒数。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-19 16:06:53 | 显示全部楼层
Twilight6 发表于 2020-8-19 16:05
你分开执行就行的

我刚试过了,可以
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-25 23:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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