我不斷收到錯誤:「UnboundLocalError:局部變量'在分配前引用的'Pitch'」有沒有解決這個問題的方法?Python:UnboundLocalError:分配之前引用的局部變量'n'
import winsound, random
Pitch = random.randint(1000, 10000)
Duration = random.randint(100, 500)
def random():
winsound.Beep(Pitch, Duration)
Pitch = random.randint(1000, 10000)
Duration = random.randint(100, 500)
winsound.Beep(Pitch, Duration)
Pitch = random.randint(1000, 10000)
Duration = random.randint(100, 500)
winsound.Beep(Pitch, Duration)
Pitch = random.randint(1000, 10000)
Duration = random.randint(100, 500)
winsound.Beep(Pitch, Duration)
Pitch = random.randint(1000, 10000)
Duration = random.randint(100, 500)
winsound.Beep(Pitch, Duration)
random()
您需要將它們標記爲全局:「全局音高,持續時間」。另外,如果您要導入模塊「隨機」,請不要將函數命名爲「random」! – senshin
只需在全局範圍刪除那些。看起來他們沒有使用。 – wim