You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Python语音识别脚本出现“BaseException is not allowed”报错

报错原因

你代码中的except语句存在语法错误:Python的异常捕获语法要求except关键字后跟随异常类本身,你当前代码写的是except speech_recognition.UnknownValueError():,末尾额外添加的括号会触发异常类的实例化,except接收到的是异常实例而非异常类型,因此触发该类型错误。

修复方案

直接删除UnknownValueError后的括号即可,修正后的完整代码如下:

import speech_recognition
import pyttsx3

recognizer = speech_recognition.Recognizer()

while True:
    try:
        with speech_recognition.Microphone() as mic:
            recognizer.adjust_for_ambient_noise(mic, duration=0.2)
            audio = recognizer.listen(mic)

            text = recognizer.recognize_google(audio)
            text = text.lower()

            print(f"Recognized {text}")

    except speech_recognition.UnknownValueError:
        recognizer = speech_recognition.Recognizer()
        continue

内容的提问来源于stack exchange,提问作者Caminero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 19:15:06