-1

Hello everyone I am trying to develop a virtual assistant which will help me with some projects so I am trying to get the basics for the final project however I get an error (UnboundLocalError: local variable 'command' referenced before assignment) and I tried to change return command to under the printf(command) but it does not let me talk and a message NONE and the other error which is (TypeError: argument of type 'NoneType' is not iterable) appears on the terminal. So how can I make this work without stop?

CODE BELOW:

import pyttsx3
import pywhatkit
import datetime

listener = sr.Recognizer()
engine = pyttsx3.init()

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

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'skor' in command:
                command = command.replace ('skor', '')
                print(command)
    except:
        pass
    return command
def run_skor():
    command = take_command()
    print(command)
    if 'play on youtube' in command:
        song = command.replace ('play on youtube', '')
        talk('playing' + song)
        pywhatkit.playonyt(song)
    elif 'search on google' in command:
        sea = command.replace ('search on google', '')
        talk ('searching' + sea)
        pywhatkit.search(sea)
    elif 'tell me the time' in command:
        time = datetime.datetime.now().strftime('%H:%M')
        print(time)
        talk('Current time is' + time)
while True:
    run_skor()```

  • Add `command=None` as first line of `take_command` or move `return` clause into `try` block. Now your command variable is not defined outside of try block if exception occurs. If you put `return` into try block, then function will return it on success or return None in case of exception (as no return clause will be reached). – STerliakov Apr 07 '21 at 14:19
  • you mean ```command=none``` below def right?I did that and it only gives me none and (TypeError: argument of type 'NoneType' is not iterable), also tried to move ```return``` into try but a new error happens (TypeError: argument of type 'NoneType' is not iterable) – Guilherme Candido Apr 07 '21 at 14:27

2 Answers2

0

The only change you need to make is in the take_command() function. This is my version of it:

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'skor' in command:
                command = command.replace ('skor', '')
                print(command)
    except sr.UnknownValueError:
        command = ""
    return command

Basically, you can first change the execpt command, to be more specific. sr has a built in command called UnknownValueError. We can use this to be more specific as to when the except command should work. Then, in except, set command to "". This will stop the errors, because when command is returned, it still is a string, and will still work.

The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
0
def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)

        try:
        print("Recognizing...")
        command = r.recognize_google(audio, language='en-in')
        print(f"User said: {command}\n")

    except Exception as e:
        print("Say that again please...")
        return "None"

    return command.lower()
TANAY T
  • 1
  • 1