简体   繁体   中英

Why do I keep getting a AttributeError: 'NoneType' object has no attribute 'lower'?

I am working on a weak Ai similar to Siri and Cortana however i have noticed i keep receiving a "AttributeError: 'NoneType' object has no attribute 'lower'", along side this instead of my code picking up my query, it always prints out 'Sorry i did not catch that'.Does anyone have any idea on how to fix this? Thanks

Errors :

if 'wikipedia' in query.lower():
AttributeError: 'NoneType' object has no attribute 'lower'

code:

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)

This is happening because takeCommand is returning None . None doesn't have a lower method, so you're getting this error.

One thing you can do is log more information about the exception being thrown in takeCommand that is causing it to enter the except block, or just remove the try block so the exception stops the program, then read the traceback.

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" # or whatever str
return query 

Tried this out myself. In Takecommand() function, where query = none, replace none with a string. Reason being when you encounter the error, Nonetype has no attribute to lower, query = none and since it isn't a str or whatever, lower() can't transform it. As mentioned above this is one way to solve this error without encountering any more. Tested response.

Otherwsie remove the query = none

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM