我为什么不断收到AttributeError:'NoneType'对象没有属性'lower'?

问题描述 投票:0回答:1

[嗨,我正在研究类似于Siri和Cortana的弱Ai,但是我注意到我一直收到“ AttributeError:'NoneType'对象没有属性'lower'””,以及它的旁边,而不是我的代码拿起我的查询,它总是打印出“对不起,我没听清楚”。有人对如何解决这个问题有任何想法吗?谢谢

错误:如果query.lower()中为'wikipedia':AttributeError:'NoneType'对象没有属性'lower'

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pythoncom

print("Initializing Karren")

MASTER = "Bob"

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

def speak(text):
    engine.say(text)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)

    if hour>=0 and hour <12:
        speak("Good Morning" + MASTER)

    elif hour>=12 and hour<18:
        speak("Good Afternoon" + MASTER)
    else:
        speak("Good Evening" + MASTER)


   # speak("I am Karren. How may I assist you?") # deliberately on not included for now

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)

    try :
        print("Recognizing...")
        query = r.recognize_google(audio, Language = 'en-uk')
        print(f"User said: {query}\n")

    except Exception as e:
        print("Sorry i didn't catch that...")
        query = None
    return query 

speak("Initializing Karren...")
wishMe()
query = takeCommand()


if 'wikipedia' in query.lower():
    speak("Searching wikipedia")
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences=2)
    speak(results)
python python-3.x artificial-intelligence speech-recognition speech-to-text
1个回答
0
投票

之所以会这样,是因为takeCommand返回了NoneNone没有lower方法,因此您遇到此错误。

© www.soinside.com 2019 - 2024. All rights reserved.