缩进错误预期缩进的块函数

时间:2019-05-27 17:49:28

标签: python

我怎么会有问题。我正在执行可以重新运行的功能

def sesver():
    r = sr.Recognizer()
    with sr.Microphone() as source:
      print("Bir sey de!")
      audio = r.listen(source)
    data = ""
    try:
      data = r.recognize_google(audio, language='tr-tr')
      data = data.lower()
      return data 
    except ValueError:

data = sesver()

4 个答案:

答案 0 :(得分:0)

我假设您的代码清单格式错误,并且函数主体实际上是缩进的。然后,您的问题出在except ValueError:中。它期望后面有一个缩进的块。如果您只是想忽略任何ValueErrors,请在缩进的块中写入pass

答案 1 :(得分:0)

尝试一下

def sesver():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print 'Bir sey de!'
        audio = r.listen(source)
    data = ''
    try:
        data = r.recognize_google(audio, language='tr-tr')
        data = data.lower()
        return data
    except ValueError: # ':' was missing
        pass           # you pass or show exception message


data = sesver()

如果您是新手,请尝试在线缩进检查器。这将有助于创建清晰的缩进代码。

答案 2 :(得分:0)

您必须为indent添加4个空格。

def sesver():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Bir sey de!")
        audio = r.listen(source)
        data = ""
        try:
            data = r.recognize_google(audio, language='tr-tr')
            data = data.lower()
            return data 
        except ValueError:
            pass

data = sesver()

答案 3 :(得分:-1)

写在代码之上:

import speech_recognition as sr

并在终端中写入:

pip install SpeechRecognition