jeudi 26 août 2021

UnboundLocalError: local variable 'speak' referenced before assignment

it shows : "UnboundLocalError: local variable 'speak' referenced before assignment" whenever i am using the speak Function. (Note: this Only Happens inside if/else )

Here is the Speak Function:

def speak(text):
    tts = gTTS(text=text, lang="en")
    audio = "SpeechAudio.mp3"
    tts.save(audio)
    playsound.playsound(audio)
    os.remove(audio) 

below is the example, where it gives error in if/else statement :

elif 'insult me' in statement:
    evil=requests.get(url='https://evilinsult.com/generate_insult.php?lang=en&type=json')
    data=evil.json()
    insult=data['insult']
    print(insult)
    speak(insult)
    time.sleep(3) 

also all these if/else statements are running under a while loop.

if i am only using :

speak("your a Nice Person")

outside the loop and if\else statements then its working fine.

Aucun commentaire:

Enregistrer un commentaire