AttributeError: 'NoneType' object has no attribute 'lower' Python3

AttributeError: 'NoneType' object has no attribute 'lower' Python3

这是一个语音助手,我想听到我的声音并打开google或搜索!但是我的程序有一个 AttributeError

我要:

1.initialization 2.speak 3.hear 和语音识别 4.Do 类似搜索的东西

我的代码:

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}\n")

    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)\Google\Chrome\Application/chrome.exe %s'
    webbrowser.get(chrome_path).open(url)
elif 'open Google' in query.lower():
    url = 'Google.com'
    chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application/chrome.exe %'
    webbrowser.get(chrome_path).open(url)
elif 'open github' in query.lower():
    url = 'github.com'
    chrome_path = 'C:\Program Files (x86)\Google\Chrome\Application/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}")


输出:

正在初始化Jarvis,哈哈哈哈!!
你好穆罕默德爵士
2020-06-16 18:21:58.364205
1399-03-27 18:21:58.370148
听....
识别...
请再说一遍
追溯(最近一次通话最后一次):
文件 "C:\Usenter code hereers\mohammad\AppData\Local\Programs\Python\Python36-32\Prject man\Main.py",第 71 行,在
中 如果 'wikipedia' 在 query.lower():
AttributeError: 'NoneType' 对象没有属性 'lower'

感谢您的帮助

您需要检查查询是否 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)\Google\Chrome\Application/chrome.exe %s'
        webbrowser.get(chrome_path).open(url)

header 中的错误是因为您正在尝试 lower() None 键入 object 并且从您的代码看来它发生在查询值中.