鱼C论坛

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

[已解决]关于Mytimer()

[复制链接]
发表于 2022-10-24 15:31:34 | 显示全部楼层 |阅读模式

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

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

x
  1. import time
  2. t1= MyTimer()
复制代码



我是看小甲鱼那个简单定制那个视频跟着敲的,但是就这两行就报错了。


  File "c:\Users\admin\Desktop\10.16\.vscode\10.16.py", line 2, in <module>
    t1= MyTimer()
NameError: name 'MyTimer' is not defined
最佳答案
2022-10-24 15:34:32
本帖最后由 jackz007 于 2022-10-24 15:43 编辑
  1. import time

  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.     def __and__(self, other):
  12.         prompt="总共运行时间"
  13.         result =[]
  14.         for index in range(6):
  15.             result.append(self.lasted[index] + other.lasted[index])
  16.             if result[index]:
  17.                 prompt += (str(result[index]) + self.unit[index])
  18.         return prompt

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

  24.     def stop(self):
  25.         if not self.begin:
  26.             print("提示:请先调用start()开始计时!")
  27.         else:
  28.             self.end = time.localtime()
  29.             self._calc()
  30.             print("计时结束")

  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.         self.begin = 0
  39.         self.end = 0

  40. t1 = MyTimer()
复制代码


       MyTimer() 是你自己写的一个类,得先有定义,才能使用。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-24 15:34:32 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-10-24 15:43 编辑
  1. import time

  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.     def __and__(self, other):
  12.         prompt="总共运行时间"
  13.         result =[]
  14.         for index in range(6):
  15.             result.append(self.lasted[index] + other.lasted[index])
  16.             if result[index]:
  17.                 prompt += (str(result[index]) + self.unit[index])
  18.         return prompt

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

  24.     def stop(self):
  25.         if not self.begin:
  26.             print("提示:请先调用start()开始计时!")
  27.         else:
  28.             self.end = time.localtime()
  29.             self._calc()
  30.             print("计时结束")

  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.         self.begin = 0
  39.         self.end = 0

  40. t1 = MyTimer()
复制代码


       MyTimer() 是你自己写的一个类,得先有定义,才能使用。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 02:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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