首页 > 解决方案 > 我想在一天中的某个时间在 python 中播放声音剪辑,但是当我使用“或”添加另一个时间段时,它会继续播放

问题描述

 import time
from datetime import datetime
while(True):
    t = datetime.now()
    tn = t.strftime("%H:%M:%S")
    time.sleep(1)
    print(tn)
    print(type(tn))
    if tn == "15:20:00":
        from pygame import mixer

        mixer.init()  # Starting the mixer

        mixer.music.load('water.wav')  # Loading the song

        mixer.music.set_volume(0.7)  # Setting the volume

        mixer.music.play(loops=-1)  # Start playing the song in loop
        inp = input("enter the defuse code")
        if inp == "drank":
            mixer.music.stop()
    else:
        pass

该程序将在 13:20:00 完美播放音乐,但如果我也想在 13:40:00 播放此声音并将 ' if tn == "15:20:00": ' 更改为 'if tn == “15:20:00”或“15:40:00”

即使时间介于这两者之间,它也不会停止播放音乐

标签: python

解决方案


推荐阅读