每次我运行旨在构建弱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")
或者,麦克风也没有拾取输入,关于为什么也有这种想法?
错误是因为变量query
有时是None
。并且您正在其上应用.lower()
功能,该功能仅适用于str
类型的对象。
您的函数未返回任何内容。因此,例如:
我正在尝试帮助您,但是我没有您要尝试做的事情的上下文,但是我给您举个例子: