首页 > 解决方案 > 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__

请告诉我代码有什么问题。

标签: pythonspeech-recognitiontext-to-speech

解决方案


尝试这个:

def takeCommmand():
    r = sr.Recognizer()
    with sr.Microphone() as source: # <--- look here
        print("listening...")
        r.pause_threshold = 1  
        audio = r.listen(source)
        return

您需要添加()它才能工作。


推荐阅读