如何退出一个函数

2024-09-29 02:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我正在做的。我是一个反应时游戏,你必须在歌曲结束后按下按钮

def play(melody, tempo, pause, pace=0.800):
    start = time.time()
    for i in range(0, len(melody)):  # Play song

        noteDuration = pace / tempo[i]
        buzz(melody[i], noteDuration)  # Change the frequency along the song 
note

        pauseBetweenNotes = noteDuration * pause
        time.sleep(pauseBetweenNotes)
        if time.time() >= start + rng:
            print("Click!!!")
            return

因此,我的问题是,当时间到达时,我用来停止歌曲的这一部分使其退出功能的速度非常慢:

        if time.time() >= start + rng:
            print("Click!!!")
            return

我的问题是:我怎样才能优化它或使它更快


Tags: theiftimesong歌曲startclickpause