Dec-12-2018, 10:00 PM
(Dec-12-2018, 09:16 PM)Ash23733 Wrote:
Error:File "C:\Users\steph\Desktop\test.py", line 23, in myCommand return command UnboundLocalError: local variable 'command' referenced before assignment
I feel that's pretty clear. You're using a variable (or at least referring to it) before you ever set it's value.
Quote:You only set it inside a try/except block, so if there's an error indef myCommand(): r = sr.Recognizer() with sr.Microphone() as source: print(' I am ready for your next command') r.pause_threshold = 1 r.adjust_for_ambient_noise(source, duration = 1) audio = r.listen(source) try: command = r.recognize_google(audio) print('You said: ' + command + '/n') except sr.UnknownValueError: assistant(myCommand()) return command
r.recognize_google()
, it never has a value. Give it a default value so that doesn't happen.>>> def spam(): ... try: ... x = 4 / 0 # divide by zero error ... except ZeroDivisionError: ... pass ... return x ... >>> spam() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 6, in spam UnboundLocalError: local variable 'x' referenced before assignment >>> def spam(): ... x = None ... try: ... x = 4 / 0 ... except ZeroDivisionError: ... pass ... return x ... >>> spam() >>>