Object has no attribute 错误使用库pyttsx3

Object has no attribute error using library pyttsx3

我已经为语音识别机器人编写了一个演示项目。但是我遇到了一些错误,表明该对象没有属性。我附上了下面的代码

def speak(audio):
    print('Computer: ' + audio)
    engine.say(audio)
     engine.runAndWait()

myCommand 的功能:

def myCommand():

r = sr.Recognizer()                                                                                   
with sr.Microphone() as source:                                                                       
    print("Listening...")
    r.pause_threshold =  1
    audio = r.listen(source)
try:
    query = r.recognize_google(audio, language='en-in')
    print('User: ' + query + '\n')

except sr.UnknownValueError:
    speak('Sorry sir! I didn\'t get that! Try typing the command!')
    query = str(input('Command: '))

    return query

主要功能:

if __name__ == '__main__':

while True:

    query = myCommand()
    print(query)
    query = query.lower()
    print(query)

    if 'open youtube' in query:
        speak('okay')
        webbrowser.open('www.youtube.com')

    elif 'open google' in query:
        speak('okay')
        webbrowser.open('www.google.co.in')          

    else:
        query = query
        speak('Searching...')
        try:
            try:
                res = client.query(query)
                results = next(res.results).text
                speak('WOLFRAM-ALPHA says - ')
                speak('Got it.')
                speak(results)

            except:
                results = wikipedia.summary(query, sentences=2)
                speak('Got it.')
                speak('WIKIPEDIA says - ')
                speak(results)

        except:
            webbrowser.open('www.google.com')

    speak('please give me Next Command! Sir!')

查询=query.lower() AttributeError: 'NoneType' 对象没有属性 'lower'

我在这里看到的问题只是代码中的缩进。

with sr.Microphone() as source:                                                                       
   print("Listening...")
   r.pause_threshold =  1
   audio = r.listen(source)
try:
query = r.recognize_google(audio, language='en-in')
print('User: ' + query + '\n')

except sr.UnknownValueError:
  speak('Sorry sir! I didn\'t get that! Try typing the command!')
  query = str(input('Command: '))

return query

在您的代码中,return 语句在 except 块内,您只需要 return 它在外面。编码愉快。