鱼C论坛

 找回密码
 立即注册
查看: 3501|回复: 12

[已解决]timer显示没被定义

[复制链接]
发表于 2020-12-30 23:34:10 | 显示全部楼层 |阅读模式

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

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

x

>>> t1 = MyTimer()
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    t1 = MyTimer()
NameError: name 'MyTimer' is not defined
>>> t1
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    t1
NameError: name 't1' is not defined
>>>
最佳答案
2021-1-6 19:58:34
whowho 发表于 2021-1-4 20:48
小甲鱼就是这样写的代码,就没问题

小甲鱼那个运行了这个程序之后输入的。。。。、

  1. import time as t

  2. class MyTimer:
  3.     def __init__(self, func, number=1000000):
  4.         self.prompt = "未开始计时!"
  5.         self.lasted = 0.0
  6.         self.default_timer = t.perf_counter
  7.         self.func = func
  8.         self.number = number
  9.    
  10.     def __str__(self):
  11.         return self.prompt

  12.     __repr__ = __str__

  13.     def __add__(self, other):
  14.         result = self.lasted + other.lasted
  15.         prompt = "总共运行了 %0.2f 秒" % result
  16.         return prompt

  17.     # 内部方法,计算运行时间
  18.     def timing(self):
  19.         self.begin = self.default_timer()
  20.         for i in range(self.number):
  21.             self.func()
  22.         self.end = self.default_timer()
  23.         self.lasted = self.end - self.begin
  24.         self.prompt = "总共运行了 %0.2f 秒" % self.lasted
  25.         
  26.     # 设置计时器(time.perf_counter() 或 time.process_time())
  27.     def set_timer(self, timer):
  28.         if timer == 'process_time':
  29.             self.default_timer = t.process_time
  30.         elif timer == 'perf_counter':
  31.             self.default_timer = t.perf_counter
  32.         else:
  33.             print("输入无效,请输入 perf_counter 或 process_time")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-30 23:49:26 | 显示全部楼层
你的MyTimer()是类吗,还是函数,MyTimer()这个定义了没
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-31 00:17:11 | 显示全部楼层
太阳总会升起 发表于 2020-12-30 23:49
你的MyTimer()是类吗,还是函数,MyTimer()这个定义了没

没有定义,按照小甲鱼课程学习写的,一样的代码但是出现问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-31 00:24:13 | 显示全部楼层
whowho 发表于 2020-12-31 00:17
没有定义,按照小甲鱼课程学习写的,一样的代码但是出现问题

没有定义,报没定义的错误不是很正常吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-31 03:48:37 | 显示全部楼层
完整的代码呢?就放一个错误代码在上面,也就只能猜测你的代码有什么 bug......
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-31 08:24:30 | 显示全部楼层
先运行MyTimer,再t1=MyTimer()

  1. import time as t

  2. class MyTimer:
  3.     def __init__(self, func, number=1000000):
  4.         self.prompt = "未开始计时!"
  5.         self.lasted = 0.0
  6.         self.default_timer = t.perf_counter
  7.         self.func = func
  8.         self.number = number
  9.    
  10.     def __str__(self):
  11.         return self.prompt

  12.     __repr__ = __str__

  13.     def __add__(self, other):
  14.         result = self.lasted + other.lasted
  15.         prompt = "总共运行了 %0.2f 秒" % result
  16.         return prompt

  17.     # 内部方法,计算运行时间
  18.     def timing(self):
  19.         self.begin = self.default_timer()
  20.         for i in range(self.number):
  21.             self.func()
  22.         self.end = self.default_timer()
  23.         self.lasted = self.end - self.begin
  24.         self.prompt = "总共运行了 %0.2f 秒" % self.lasted
  25.         
  26.     # 设置计时器(time.perf_counter() 或 time.process_time())
  27.     def set_timer(self, timer):
  28.         if timer == 'process_time':
  29.             self.default_timer = t.process_time
  30.         elif timer == 'perf_counter':
  31.             self.default_timer = t.perf_counter
  32.         else:
  33.             print("输入无效,请输入 perf_counter 或 process_time")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-31 08:46:42 | 显示全部楼层
  1. import time
  2. t1 = time.time()
  3. print(t1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-4 20:48:03 | 显示全部楼层
太阳总会升起 发表于 2020-12-30 23:49
你的MyTimer()是类吗,还是函数,MyTimer()这个定义了没

小甲鱼就是这样写的代码,就没问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-4 20:48:58 | 显示全部楼层
whowho 发表于 2020-12-31 00:17
没有定义,按照小甲鱼课程学习写的,一样的代码但是出现问题

小甲鱼这样写的代码,运行就没问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-4 20:50:15 | 显示全部楼层
逃兵 发表于 2020-12-31 08:24
先运行MyTimer,再t1=MyTimer()

小甲鱼就是这样写的代码就没问题,为什么我的就出错,小甲鱼没有import  time
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-4 23:45:54 | 显示全部楼层
whowho 发表于 2021-1-4 20:50
小甲鱼就是这样写的代码就没问题,为什么我的就出错,小甲鱼没有import  time

小甲鱼在哪儿写的没定义?第几节,多少分多少秒?还是课后作业?

你看看他是不是在 IDLE 里面跑的,如果是的话,可能是之前的某个地方已经引用过了time模块。没有引用按道理来讲是会出现未定义的情况。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-1-5 08:06:51 | 显示全部楼层
whowho 发表于 2021-1-4 20:50
小甲鱼就是这样写的代码就没问题,为什么我的就出错,小甲鱼没有import  time

小甲鱼在文本模式下运行,进入交互模式,之后在交互模式下输入t1=MyTimer()
所以你没看到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-1-6 19:58:34 | 显示全部楼层    本楼为最佳答案   
whowho 发表于 2021-1-4 20:48
小甲鱼就是这样写的代码,就没问题

小甲鱼那个运行了这个程序之后输入的。。。。、

  1. import time as t

  2. class MyTimer:
  3.     def __init__(self, func, number=1000000):
  4.         self.prompt = "未开始计时!"
  5.         self.lasted = 0.0
  6.         self.default_timer = t.perf_counter
  7.         self.func = func
  8.         self.number = number
  9.    
  10.     def __str__(self):
  11.         return self.prompt

  12.     __repr__ = __str__

  13.     def __add__(self, other):
  14.         result = self.lasted + other.lasted
  15.         prompt = "总共运行了 %0.2f 秒" % result
  16.         return prompt

  17.     # 内部方法,计算运行时间
  18.     def timing(self):
  19.         self.begin = self.default_timer()
  20.         for i in range(self.number):
  21.             self.func()
  22.         self.end = self.default_timer()
  23.         self.lasted = self.end - self.begin
  24.         self.prompt = "总共运行了 %0.2f 秒" % self.lasted
  25.         
  26.     # 设置计时器(time.perf_counter() 或 time.process_time())
  27.     def set_timer(self, timer):
  28.         if timer == 'process_time':
  29.             self.default_timer = t.process_time
  30.         elif timer == 'perf_counter':
  31.             self.default_timer = t.perf_counter
  32.         else:
  33.             print("输入无效,请输入 perf_counter 或 process_time")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 08:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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