|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
import time as t
class tt:
def __init__(self):
self.unit=['年','月','天','小时','分','秒']
self.prompt='未开始计时'
self.last=[]
self.begin=0
self.end=0
def __str__(self):
return self.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.last=[]
self.prompt='总共运行了'
for index in range(6):
self.last.append(self.end[index]-self.begin[index])
if self.last[index]:
self.prompt+=(str(self.last[index]+self.unit[index]))
t1=tt()
我用的是pycharm运行,但是我发现不能像shell里面运行的那样,先t1.begin(),过一会在t1.end()就出结果。
我先调用t1.begin(),然后它就停止运行了,再调用t1.end()就会触发代码中的错误'请先调用start()进行计时'
有大佬解释一下是怎么回事吗?
只能在 IDLE Shell 中运行代码。先把你的代码用 IDLE 运行一遍(不要写 t1 = tt() 以及后面的语句),然后再在打开的 Shell 窗口中计时(t1.start() 、t1.stop())
|
-
|