Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 如何根据命令运行计时器?_Python_Python 3.x_Timer - Fatal编程技术网

Python 如何根据命令运行计时器?

Python 如何根据命令运行计时器?,python,python-3.x,timer,Python,Python 3.x,Timer,我一直在用这个计时器,但无法使它工作。基本上,我希望它在时间到的时候播放我系统中的歌曲。我一直在尝试编写这个程序,但我不明白为什么我的代码不能按我希望的方式运行。有人能指出其中是否有错误吗 我认为您的示例没有正确使用while(和return)。一个更简单的版本怎么样: 导入时间 导入操作系统 T=int(输入(“为计时器输入所需时间-”) def Timer(): 时间。睡眠(T) 操作系统(“启动C:/Users/Public/Music/Sample Music”) 计时器() 您的代码有

我一直在用这个计时器,但无法使它工作。基本上,我希望它在时间到的时候播放我系统中的歌曲。我一直在尝试编写这个程序,但我不明白为什么我的代码不能按我希望的方式运行。有人能指出其中是否有错误吗

我认为您的示例没有正确使用
while
(和
return
)。一个更简单的版本怎么样:

导入时间
导入操作系统
T=int(输入(“为计时器输入所需时间-”)
def Timer():
时间。睡眠(T)
操作系统(“启动C:/Users/Public/Music/Sample Music”)
计时器()

您的代码有什么问题,特别是这种情况

(t.tm_sec)!=T+(T.tm_秒)
问题是当您执行
(t.tm_sec)!=T+(T.tm\u秒)
。设置后,将使用相同的t值。我认为您假设在while语句中每次都会重新计算
t
。要每次都重新计算
t
,请执行以下操作:

导入时间
导入操作系统
T=int(输入(“为计时器输入所需时间-”)
snap\u time=time.localtime(time.time())
def Timer():
t=time.localtime(time.time())
当t.tm_秒<(t+snap_time.tm_秒):
t=time.localtime(time.time())
操作系统(“启动C:/Users/Public/Music/Sample Music”)
计时器()

t.tm_sec
是固定的,因此除非
t
为0,否则条件将永远不会为
False
,因此将永远不会执行
else
块。除此之外,循环中还有
return
,这意味着它将只运行一次迭代并退出函数。试一试

import time
import os

T = int(input("Enter desired time for the timer - "))
t = time.localtime(time.time())

def Timer():
    while ((t.tm_sec) != T+(t.tm_sec)):
        return t
    else:
        os.system("start C:/Users/Public/Music/Sample Music")


Timer()

您面临的确切错误/问题/错误行为是什么?您希望
(t.tm_sec)
何时等于
t+(t.tm_sec)
?如果
T
为非零,则永远不会为真。此外,一旦点击
return
,您的
Timer()
函数将结束,程序也将完成。
T = int(input("Enter desired time for the timer - ")) + time.time()

def Timer():
    while time.time() < T:
        pass
    else:
        os.system("start C:/Users/Public/Music/Sample Music")
def Timer():
    while time.time() < T:
        pass
    os.system("start C:/Users/Public/Music/Sample Music")