AttributeError:'NoneType'对象没有属性'lower'Python3 [英] AttributeError: 'NoneType' object has no attribute 'lower' Python3

查看:204
本文介绍了AttributeError:'NoneType'对象没有属性'lower'Python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要:

1.初始化2.说话3.听觉和言语识别4.做类似搜索的事情

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

我的代码:

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


输出:

初始化贾维斯,哈哈哈哈!!
您好,穆罕默德爵士
2020-06-16 18:21:58.364205
1399-03-27 18:21:58.370148
听....
认识...
再说一次
追溯(最近一次通话):
文件"C:\ Us 在此处输入代码 mohammad \ AppData \ Local \ Programs \ Python \ Python36-32 \ Prject man \ Main.py",第71行,在
如果query.lower()中的维基百科":
AttributeError:'NoneType'对象没有属性'lower'

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 hereers\mohammad\AppData\Local\Programs\Python\Python36-32\Prject man\Main.py", line 71, in
if 'wikipedia' in query.lower():
AttributeError: 'NoneType' object has no attribute 'lower'

感谢您的帮助

推荐答案

您需要检查查询是否不是无

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)\Google\Chrome\Application/chrome.exe %s'
        webbrowser.get(chrome_path).open(url)

标头中的错误是因为您尝试 lower()无类型对象,并且从您的代码看来,它已在查询值中发生.

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.

这篇关于AttributeError:'NoneType'对象没有属性'lower'Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆