-2

I have seen similar questions about my problem but they do not seem to help.

My code is

def take_command(): 
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'give' in command:
                print('give mentioned')
                comand = command.replace('give' '')
                print(comand)
    except:
        pass
    return command


def run_give():
    command = take_command()
    print(command)

The error is

Traceback (most recent call last):
  File "c:\Users\REDACTED\REDACTED\REDACTED\REDACTED\main.py", line 72, in <module>
    run_give()
  File "c:\Users\REDACTED\REDACTED\REDACTED\REDACTED\main.py", line 38, in run_give
    command = take_command()
  File "c:\Users\REDACTED\REDACTED\REDACTED\REDACTED\main.py", line 34, in take_command
    return command
UnboundLocalError: local variable 'command' referenced before assignment

I have tried many ways to fix it but nothing works. And yes I have these istalled for it to work

import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyjokes 
hyuhyuhyu
  • 1
  • 1
  • 1
    Where do you think `command` is given a value? What have you done to verify that that is actually happening? – Scott Hunter May 09 '22 at 00:39
  • You should find your answer here: [What is wrong with using a bare 'except'?](https://stackoverflow.com/q/54948548/4518341) and ["UnboundLocalError: local variable referenced before assignment" after an if statement](https://stackoverflow.com/q/15367760/4518341). You're not using an if-statement of course, but it's the same idea: `command` is undefined under some conditions. – wjandrea May 09 '22 at 00:39
  • I closed your question under the existing ones since it looks like they fully address your problem, but LMK if not. BTW, welcome to Stack Overflow! Check out the [tour] and [ask]. For debugging help in the future, you'll need to make a [mre], meaning complete but minimal code. – wjandrea May 09 '22 at 00:46

0 Answers0