猿问

多个elif语句的Python顺序

我通过将与捕获音频无关的语句从 with 语句中移出来让它工作,这应该对你有用:


import speech_recognition as sr


rec = sr.Recognizer()


print('say something')

with sr.Microphone() as source:

    audio = rec.listen(source)

    voice_data = rec.recognize_google(audio)


print(voice_data)


慕雪6442864
浏览 116回答 1
1回答

跃然一笑

是的,if-else 语句是顺序执行的,这意味着 python 将运行 if 语句,如果它为假,那么它将运行下一个 elif,依此类推,直到仅当上述所有条件都为假时才执行的 else 语句。
随时随地看视频慕课网APP

相关分类

Python
我要回答