Python-语音识别:属性错误:__ENTER__ [英] Python - Speech Recognition: Attribute error : __enter__

查看:0
本文介绍了Python-语音识别:属性错误:__ENTER__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用语音识别和pyttsx3模块在python中编写Jarvis代码。我因为Attribute error:__enter__的错误而卡住了 我不明白为什么会出现这样的情况。我对蟒蛇还是个新手。我目前使用的是python3.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

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

这篇关于Python-语音识别:属性错误:__ENTER__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆