简体   繁体   中英

How do i fix UnboundLocalError: local variable 'command' referenced before assignment

I'm trying to make a virtual assistant and the only problem left is this one. I tried google and reddit but I dint get any answers. I don't know how to fix it but here is the code:

I'm typing this because the stack overflow wont let me post unless i have more text so here is my life story. Last week in school we did an experiment where we had to drink 5 litters water and then stand i freezing weather for 10 minutes with barley any clothes. We had to see if standing in cold would make us want to pee more then being inside. the theory was that blood vanes would contract and there would not be any space for water so we would pee it out. No one had to pee until next lesson, then everyone went to the bathroom every 10 minutes.

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)# 0 is male, 1 is female
engine.say('Booting Lora system')
engine.runAndWait()

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

def take_command():
    try:
        with sr.Microphone() as source:
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
    except:
        pass
    return command


def run_lora():
    command = take_command()
    if 'play' in command:
        song = command.replace('play', '')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.now().strftime('%H:%M')
        talk('Current time is ' + time)
    elif 'who' in command:
        question = command
        info = wikipedia.summary(question, 3)
        talk(info)
    elif 'tell' in command and 'joke' in command:
        talk(pyjokes.get_joke())
    else:
        talk('Sorry I did not understand')

while True:
    run_lora()

Because you defined command in run_lora it is a private variable you just need to add global command .


def take_command():

    global command    # <---- Over Here #############

    try:
        with sr.Microphone() as source:
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
    except:
        pass
    return command

Change the word command to cmnd in take_command() function. Your command variable is referenced before it was actually declared


def take_command():
    try:
        with sr.Microphone() as source:
            voice = listener.listen(source)
            cmnd = listener.recognize_google(voice)
            cmnd = cmnd.lower()
    except:
        pass
    return cmnd

I think my problem was that I needed pyadio but the pyaudio version is way to old for python 3.8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM