python - Python - 语音识别:属性错误:__enter__
问题描述
我正在尝试使用语音识别和 pyttsx3 模块在 python 中编写 jarvis 代码。我因为一个错误而卡住了,它说Attribute error:__enter__
我不明白为什么会这样。我是 python 新手。目前我正在使用python 3.8。
def takeCommmand():
r = sr.Recognizer()
with sr.Microphone as source:
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
return
错误:
File "c:/Users/Dell/Desktop/jarvis voice assistant/jarvis.py", line 28, in takeCommmand
with sr.Microphone as source:
AttributeError: __enter__
请告诉我代码有什么问题。
解决方案
尝试这个:
def takeCommmand():
r = sr.Recognizer()
with sr.Microphone() as source: # <--- look here
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
return
您需要添加()
它才能工作。
推荐阅读
- ruby - Activerecord has_and_belongs_to_many 与嵌套组
- java - 加载“旧”Android Studio 项目 - 在 Studio 更新后,它将不再打开/编译,要求更新版本?
- php - 如何将日期转换为strtotime?
- c# - UWP MediaPlaybackList 在列表项之间弹出声音
- html - 如何在 HTML 中手动设置输入类型日期值
- python - pyodbc 受信任的连接在 IIS 上不起作用
- git - 如何清理和准备一个我想公开的 git repo?
- python - 如何在vscode中检查python的代码源代码(父)
- reactjs - 尝试通过覆盖默认属性来自定义 .slick-next 和 .slick-prev 但我的 CSS 没有注册。为什么?怎么修?
- oracle - 这个包的目的是什么?