Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I noticed that in the take_command() function of the script, the command variable was being used before being assigned a value, which caused an error. #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Abreeq
Copy link

@Abreeq Abreeq commented Jun 22, 2023

I wanted to contribute a bug fix to the existing codebase. I noticed that in the take_command() function of the script, the command variable was being used before being assigned a value, which caused an error.

To resolve this issue, I added an initialization step to the command variable, assigning it an empty string at the beginning of the function. This ensures that the variable is always defined, even if no voice command is captured.

I have tested the updated code, and it now runs without any issues. I believe this fix will enhance the functionality and reliability of the script.

Thank you for maintaining this project, and I appreciate the opportunity to contribute. Please feel free to review and merge this bug fix at your convenience.

… that in the take_value() function of the script, the command variable was being used before being assigned a value, which caused an error.

To resolve this issue, I added an initialization step to the command.
@AtharvShinde2004
Copy link

The error you're encountering, "UnboundLocalError: cannot access local variable 'command' where it is not associated with a value," is occurring because the variable command is defined within the try block in the take_command function, and there's a possibility that it may not be assigned a value if an exception occurs. In Python, you cannot access a variable before it's assigned a value, and this error is raised when you try to do so.

To fix this issue, you can initialize the command variable with an empty string before the try block, like this:

def take_command():
    command = ""  # Initialize command with an empty string
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'alexa' in command:
                command = command.replace('alexa', '')
                print(command)
    except:
        pass
    return command

This way, even if an exception occurs, the command variable will still have a value (an empty string), and you won't encounter the UnboundLocalError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants