Python语音助手
所以我写了这个函数来得到我所说的:
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 2
audio = r.listen(source)
try:
query = r.recognize_google(audio, language='en')
except Exception as e:
speak("Say that again please...")
pass
return query
然后当 True 时,函数像这样运行:
query = takeCommand().lower()
但我得到这个错误: 赋值前引用的局部变量“query”
So i wrote this function to get what i say:
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 2
audio = r.listen(source)
try:
query = r.recognize_google(audio, language='en')
except Exception as e:
speak("Say that again please...")
pass
return query
and then while True the function is running like this:
query = takeCommand().lower()
but i get this error:
local variable 'query' referenced before assignment
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码遇到异常情况并且未定义
查询
,请尝试以下操作:Your code is running into the exception condition and not defining
query
try this: