【问题标题】:How to fix "BaseException is not allowed"如何修复“不允许BaseException”
【发布时间】:2021-08-13 23:21:27
【问题描述】:

我试图让这个脚本得到我所说的并在终端上打印出来,但我得到了这个错误

TypeError: catching classes that do not inherit from BaseException is not allowed

为此,我遵循了一个教程,他在运行 3.9.5 版本时工作得很好

 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

【问题讨论】:

    标签: python-3.x exception speech-recognition except


    【解决方案1】:

    你的

    except speech_recognition.UnknownValueError():
    

    应该是

    except speech_recognition.UnknownValueError:
    

    即它应该命名类型,而不是调用它并使用返回值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-06
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多