Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计时器Python 3,3_Python_Python 3.3 - Fatal编程技术网

计时器Python 3,3

计时器Python 3,3,python,python-3.3,Python,Python 3.3,我想知道如何使我在Python3.3中的时间以分秒的形式显示出来。在分钟时,它仅以秒为单位显示?非常感谢 start_time=time.time() # program operation end_time=time.time()-start_time print(end_time) 您可以使用datetime模块 import datetime end_time = 400 # this is the difference in your example, in seconds st

我想知道如何使我在Python3.3中的时间以分秒的形式显示出来。在分钟时,它仅以秒为单位显示?非常感谢

start_time=time.time()

# program operation

end_time=time.time()-start_time
print(end_time)

您可以使用
datetime
模块

import datetime
end_time = 400 # this is the difference in your example, in seconds

str(datetime.timedelta(seconds = end_time))
结果

'0:06:40'

使用datetime模块。有些人使用带有sleep()函数的时间模块,但它不准确。根据你在这方面的经验,你应该使用时间模块

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”。

这类似于,您知道如何在分钟和秒之间转换吗?我将使用什么函数来启动计时器,而不是使用start_time=time.time()@user3565969为什么需要不同的函数?在操作前后使用
time.time()?这就是函数的来源。我有import datetime