import datetime
from pydub.playback import play
currentDate = datetime.datetime.now()
times = [12, 19, 23]
minutes = [53, 52, 51]
while True:
for i in times:
for j in minutes:
if(currentDate.hour == i and currentDate.minute == j):
# print(os.getcwd())
index = minutes.index(j)+1;
print(index)
announce = AudioSegment.from_mp3(
f'{os.getcwd()}\\audios\\announcement_{index}.mp3')
play(announce)
即使 if 块中的条件不正确,此代码也会继续播放“通知”,任何人都可以告诉我我在这里应用的错误逻辑是什么......任何类型的帮助都会非常有帮助
“时间和分钟都是列表”
答案 0 :(得分:1)
所以问题在于您没有更新 CurrentDate
变量。
如果您在条件为真时运行程序,这将导致无限循环。
所以要解决这个问题,你只需要更新那个变量:
times = [12, 19, 23]
minutes = [53, 52, 51]
while True:
# Updating the date every frame
currentDate = datetime.datetime.now()
for i in times:
for j in minutes:
if(currentDate.hour == i and currentDate.minute == j):
# print(os.getcwd())
index = minutes.index(j)+1;
print(index)
announce = AudioSegment.from_mp3(
f'{os.getcwd()}\\audios\\announcement_{index}.mp3')
play(announce)
答案 1 :(得分:1)
因为有一个 var
。这意味着除非您 while True
它,否则它将无限循环。我不知道你为什么想要一个无限循环,但这应该可以解决问题:
break