python中的语音助手-未绑定的本地错误

问题描述

我试图使用一些 youtube 视频和 我收到此错误 发生异常:UnboundLocalError 赋值前引用的局部变量“command”

我不明白为什么我会收到这个错误以及我应该如何修复它

这是代码

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)

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

 def run_ana():                     
     command = take_command()  #command taken
     print(command)     
    if 'play' in command:
        song = command.replace('play',' ')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in command:
        time = datetime.datetime.Now().strftime('%I:%M %p')
        talk('Current time is ' + time)
    elif 'who the heck is' in command:
        person = command.replace('who the heck is','')
        info = wikipedia.summary(person,1)
        print(info)
        talk(info)
    elif 'Friend' in command:
        talk('I am your best friend buddy')
    elif 'Hello' in command:
        talk('Hello I am Ana,nice to meet you')
    elif 'joke' in command:
        talk(pyjokes.get_joke())
    else:
        talk('Please say the command again.') 

def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'ana' in command:
                command = command.replace('ana','')
                print(command)
    except:
        pass
   return command    #`enter code here`here i am getting the error 

while True:
    run_ana()```


*****Please HelP***********

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)