简体   繁体   English

如何修复 UnboundLocalError:分配前引用的局部变量“命令”

[英]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.我尝试了 google 和 reddit,但我没有得到任何答案。 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.上周在学校里我们做了一个实验,我们必须喝 5 升水,然后在寒冷的天气里用大麦和衣服等 10 分钟。 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.直到下节课才开始小便,然后每个人每 10 分钟去一次洗手间。

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 .因为你在run_lora中定义了command它是一个私有变量,你只需要添加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.take_command() function 中将 word command更改为cmnd 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我认为我的问题是我需要 pyadio 但 pyaudio 版本对于 python 3.8 来说太旧了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复我的代码中的“UnboundLocalError:分配前引用的局部变量“dc”? - How do I fix 'UnboundLocalError: local variable 'dc' referenced before assignment' in my code? ' UnboundLocalError: 赋值前引用的局部变量 'command' ' - ' UnboundLocalError: local variable 'command' referenced before assignment ' 如何修复“UnboundLocalError:赋值前引用的局部变量‘books’”? - How to fix "UnboundLocalError: local variable 'books' referenced before assignment"? 如何修复 UnboundLocalError:在 Python 中分配之前引用的局部变量“df” - How to fix UnboundLocalError: local variable 'df' referenced before assignment in Python UnboundLocalError:赋值前引用了局部变量“i” - UnboundLocalError: local variable 'i' referenced before assignment 赋值之前引用了unboundlocalerror局部变量&#39;i&#39; - unboundlocalerror local variable 'i' referenced before assignment 您如何解决:“UnboundLocalError:分配前引用的局部变量‘事件’”在 pygame 中? - How do you fix: "UnboundLocalError: local variable 'events' referenced before assignment" in pygame? 我该如何解决:UnboundLocalError:分配前引用的局部变量“生成” - how can i fix: UnboundLocalError: local variable 'generate' referenced before assignment 如何修复“UnboundLocalError:分配前引用的局部变量'user_score'”? - How can I fix "UnboundLocalError: local variable 'user_score' referenced before assignment"? 如何解决此错误“UnboundLocalError:分配前引用的局部变量'a'” - How can I fix this error “UnboundLocalError: local variable 'a' referenced before assignment”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM