请帮我解决这个奇怪的 UnboundLocalError
pls help me fix this weird UnboundLocalError
我尝试制作一个 Alexa Vitual Assistant,但代码抛出一个 UnboundLocalError
。请帮助
import speech_recognition as sr
import pyttsx3
import datetime
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty('voice', voices[1].id)
def talk(text):
engine.say(text)
engine.runAndWait()
talk("Hi i'm Alexa ,your assistant")
talk("How can i help you ?")
print("Hi i'm Alexa")
def take_request():
try:
with sr.Microphone() as source:
talk("i am listening")
print("listening ....")
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
#command = input("say sth : ")
commands= command.lower()
if "a" in commands:
print(commands)
except:
pass
return commands
def run_alexa():
#global commands
cm = take_request()
if "play" in cm:
print("playing music")
talk("playing music")
elif "time" in cm:
time = datetime.datetime.now().strftime("%H:%M")
print(time)
talk("your current time is "+time)
else:
talk('Please say the command again.')
print("pls talk again !!!")
while True:
run_alexa()
和错误
Hi i'm Alexa
listening ....
Traceback (most recent call last):
File "C:\Users\USER\Desktop\autobot\Hana assistant.py", line 50, in <module>
run_alexa()
File "C:\Users\USER\Desktop\autobot\Hana assistant.py", line 36, in run_alexa
cm = take_request()
File "C:\Users\USER\Desktop\autobot\Hana assistant.py", line 32, in take_request
return commands
UnboundLocalError: local variable 'commands' referenced before assignment
我可以手写命令而且没有错误!
我尝试 global commands
但它不起作用
如果出现异常,您将不会分配 commands
。您可以在 try
块之前分配默认值。
def take_request():
commands = ""
try:
with sr.Microphone() as source:
talk("i am listening")
print("listening ....")
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
#command = input("say sth : ")
commands= command.lower()
if "a" in commands:
print(commands)
except:
pass
return commands
我尝试制作一个 Alexa Vitual Assistant,但代码抛出一个 UnboundLocalError
。请帮助
import speech_recognition as sr
import pyttsx3
import datetime
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty("voices")
engine.setProperty('voice', voices[1].id)
def talk(text):
engine.say(text)
engine.runAndWait()
talk("Hi i'm Alexa ,your assistant")
talk("How can i help you ?")
print("Hi i'm Alexa")
def take_request():
try:
with sr.Microphone() as source:
talk("i am listening")
print("listening ....")
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
#command = input("say sth : ")
commands= command.lower()
if "a" in commands:
print(commands)
except:
pass
return commands
def run_alexa():
#global commands
cm = take_request()
if "play" in cm:
print("playing music")
talk("playing music")
elif "time" in cm:
time = datetime.datetime.now().strftime("%H:%M")
print(time)
talk("your current time is "+time)
else:
talk('Please say the command again.')
print("pls talk again !!!")
while True:
run_alexa()
和错误
Hi i'm Alexa
listening ....
Traceback (most recent call last):
File "C:\Users\USER\Desktop\autobot\Hana assistant.py", line 50, in <module>
run_alexa()
File "C:\Users\USER\Desktop\autobot\Hana assistant.py", line 36, in run_alexa
cm = take_request()
File "C:\Users\USER\Desktop\autobot\Hana assistant.py", line 32, in take_request
return commands
UnboundLocalError: local variable 'commands' referenced before assignment
我可以手写命令而且没有错误!
我尝试 global commands
但它不起作用
如果出现异常,您将不会分配 commands
。您可以在 try
块之前分配默认值。
def take_request():
commands = ""
try:
with sr.Microphone() as source:
talk("i am listening")
print("listening ....")
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
#command = input("say sth : ")
commands= command.lower()
if "a" in commands:
print(commands)
except:
pass
return commands