YouTube ビデオを使用して音声アシスタントを作成しようとしていましたが、このエラー例外が発生しました: UnboundLocalError ローカル変数 'コマンド' が割り当て前に参照されました
このエラーが発生する理由と修正方法がわかりません
これがコードです
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***********