Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

python 3.x - AttributeError: 'NoneType' object has no attribute 'lower' Python3

This is a voice assitance and i want hear my voice and open google or searching! but My program has a AttributeError

I want :

1.initialization 2.speak 3.hear and speech Recognization 4.Do Something Like searching

My Code:

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import jdatetime
import persian

Boss = 'Mohamaad'
print('Hello sir %s' % Boss)
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)

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

speak('Hello sir %s' % Boss)

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.record(source,duration=2)
        speak('I am Listening sir')
        print("Listening ....")
        audio = r.listen(source)

    try :
         print("Recognizing...")
         query = r.recognize_google(audio, Language ='en-us')
         print(f"user said: {query}
")

    except Exception as e:
        print("Say that again please")
        speak('Say that again please')
        query=None

    return query

wishMe()
query = takeCommand()



#Logic for executing tasks as per the query
if 'wikipedia' in query.lower():
    speak('searching in wikipedia....')
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences =2)
    print(results)
    speak(results)

elif 'open youtube' in query.lower():
    url = 'youtube.com'
    chrome_path = 'C:Program Files (x86)GoogleChromeApplication/chrome.exe %s'
    webbrowser.get(chrome_path).open(url)
elif 'open Google' in query.lower():
    url = 'Google.com'
    chrome_path = 'C:Program Files (x86)GoogleChromeApplication/chrome.exe %'
    webbrowser.get(chrome_path).open(url)
elif 'open github' in query.lower():
    url = 'github.com'
    chrome_path = 'C:Program Files (x86)GoogleChromeApplication/chrome.exe %'
    webbrowser.get(chrome_path).open(url)
elif 'Play music' in query.lower():
    songs_dir = "C:\Users\mohmmad\Downloads\Music"
    songs = os.listdir(songs_dir)
    speak(songs)
    os.startfile(os.path.join(songs_dir,songs[0]))

elif 'time' in query():
    strTime = datetime.datetime.now().strftime("%H:%M:%S")
    speak(f"{Boss} the time is {strTime}")


Output:

Initializing Jarvis, haa haa haa!!
Hello sir Mohamaad
2020-06-16 18:21:58.364205
1399-03-27 18:21:58.370148
Listening ....
Recognizing...
Say that again please
Traceback (most recent call last):
File "C:Usenter code hereersmohammadAppDataLocalProgramsPythonPython36-32Prject manMain.py", line 71, in
if 'wikipedia' in query.lower():
AttributeError: 'NoneType' object has no attribute 'lower'

thanks for your helping

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to check if the query is not None

if query:
    if 'wikipedia' in query.lower():
        speak('searching in wikipedia....')
        query = query.replace("wikipedia", "")
        results = wikipedia.summary(query, sentences =2)
        print(results)
        speak(results)

    elif 'open youtube' in query.lower():
        url = 'youtube.com'
        chrome_path = 'C:Program Files (x86)GoogleChromeApplication/chrome.exe %s'
        webbrowser.get(chrome_path).open(url)

The error that in the header is because you are trying to lower() None type object and from your code it seems that it's happened in the query value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...