我如何修复AttributeError:'NoneType'对象没有属性'lower'?

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

每次我运行旨在构建弱Ai平台的代码时,我都会收到一个AttributeError:'NoneType'对象没有属性'lower',我完全不知道为什么它可以在Ai平台上正常工作我正在关注的教程。有人可以指导我解决此问题,因为我是python的新手。谢谢

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

print("Initializing Bot")

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("How may I assist you?")

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-in')
        print(f"user said: {query}\n")

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

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

#Logic

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


if 'open youtube' in query.lower():
    webbrowser.open("youtube.com")

或者,麦克风也没有拾取输入,关于为什么也有这种想法?

python python-3.x artificial-intelligence speech-recognition speech-to-text
3个回答
0
投票

错误是因为变量query有时是None。并且您正在其上应用.lower()功能,该功能仅适用于str类型的对象。


0
投票

您的函数未返回任何内容。因此,例如:


0
投票

我正在尝试帮助您,但是我没有您要尝试做的事情的上下文,但是我给您举个例子:

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