我想知道如何在几分钟和几秒钟内用Python 3.3显示我的时间。在那一刻,它只在几秒钟内显示?非常感谢。
start_time=time.time()
# program operation
end_time=time.time()-start_time
print(end_time)
答案 0 :(得分:1)
您可以使用datetime
模块
import datetime
end_time = 400 # this is the difference in your example, in seconds
str(datetime.timedelta(seconds = end_time))
结果
'0:06:40'
答案 1 :(得分:0)
使用日期时间模块。有些人使用带有 sleep() 函数的 time 模块,但它不准确。根据您的经验水平,您应该使用时间模块。
import time
seconds = 0
minutes = 0
while True:
print('\r'f'{minutes}:{seconds}', end='')
time.sleep(1)
seconds += 1
if seconds == 60:
minutes += 1
seconds = 0
elif minutes == 2:
break*
print 语句中的 '\r' 是将终端中的打印光标带到行首。 Python 默认情况下向字符串添加一个换行符(\n,光标转到下一行。),因此,为字符串设置 'end = none'。