我需要能夠在特定時間段後切換布爾值,而其餘代碼仍照常運行。代碼的主要部分發生了什麼取決於Bool的值。在特定的時間量後更改變量python
這是我的嘗試與goodguy的建議,但我仍然無法得到它的工作。當我打電話給班級時,「播放」切換爲True,但在2秒後不會切換回False,因此音調只播放一次。我究竟做錯了什麼?
class TimedValue:
def __init__(self):
self._started_at = datetime.datetime.utcnow()
def __call__(self):
time_passed = datetime.datetime.utcnow() - self._started_at
if time_passed.total_seconds() > 2:
return False
return True
playing = False
while True:
trigger = randint(0,10) # random trigger that triggers sound
if trigger == 0 and playing == False:
#play a tone for 2 seconds whilst the random triggers continue running
#after the tone is over and another trigger happens, the tone should play again
thread.start_new_thread(play_tone, (200, 0.5, 2, fs, stream,))
value = TimedValue()
playing = value()
time.sleep(0.1)
你能包含的代碼你迄今爲止最好的嘗試?如果有起始基礎,理解這個問題要容易得多。 – roganjosh
那好嗎?我沒有一個最好的嘗試,沒有任何工作。 – ElanaLS