-1

I was trying to create a voice Assistant using some youtube video and I am getting this Error Exception has occurred: UnboundLocalError local variable 'command' referenced before assignment

I am not getting why I'm getting this error and how should I fix it

Here's the code

import speech_recognition as sr       
import pyttsx3  
import pywhatkit  
import datetime  
import wikipedia  
import pyjokes 

listener = sr.Recognizer()  
engine = pyttsx3.init()  
voices = engine.getProperty('voices')  
engine.setProperty('voice', voices[1].id)

def talk(text):   
    engine.say(text)    
    engine.runAndWait()    
                        

 def run_ana():                     
     command = take_command()  #command taken
     print(command)     
    if 'play' in command:
        song = command.replace('play', ' ')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        talk('Current time is ' + time)
    elif 'who the heck is' in command:
        person = command.replace('who the heck is', '')
        info = wikipedia.summary(person, 1)
        print(info)
        talk(info)
    elif 'Friend' in command:
        talk('I am your best friend buddy')
    elif 'Hello' in command:
        talk('Hello I am Ana, nice to meet you')
    elif 'joke' in command:
        talk(pyjokes.get_joke())
    else:
        talk('Please say the command again.') 

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'ana' in command:
                command = command.replace('ana', '')
                print(command)
    except:
        pass
   return command    #`enter code here`here i am getting the error 

while True:
    run_ana()```


*****Please Help***********
  • Don't use all-catch try/except in `take_command` and you will see what the error is. now you just pass on error and then attempt to return `command` that does not exists. – buran Dec 19 '20 at 20:16
  • Fix your code formatting and indentation. Post the traceback of the error. – Mark Tolonen Dec 19 '20 at 20:17
  • in your try/except block you aren't doing anything, so your code is failing before return statement, but it's doing it silently. If you are expecting errors in this code block, create `command` variable before `try` block with None value. This way if voice capture and analysis fail, the function will return `None`, instead of exception. I would recommend at least logging/printing what exceptions are you catching in your try block, instead of juss passing over them – w8eight Jan 27 '23 at 08:29

1 Answers1

-2

by installing some libraries it can soleved

pip install microphone

  • I'm fairly sure this answer is wrong. If it is correct, it can be improved to meet the sites terms by explaining how installing that library will solve an unbound local error in the main program. – Sam Hartman Jan 30 '23 at 23:13
  • if this not works use return ' ' instead of pass – ANANDA KUMAR A Feb 01 '23 at 04:27