【问题标题】:How do i fix AttributeError: 'NoneType' object has no attribute 'lower'?如何修复 AttributeError:'NoneType' 对象没有属性 'lower'?
【发布时间】:2020-04-06 19:39:51
【问题描述】:

每次我运行旨在构建弱 Ai 平台的代码时,我都会收到 AttributeError: 'NoneType' object has no attribute 'lower',我完全不知道为什么会这样在我正在关注的教程中工作正常。有人可以指导我解决这个问题,因为我对 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


    【解决方案1】:

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

    您可以通过将代码放入 if 循环中来控制这一点,该循环仅在查询变量中有字符串时运行。可能是这样的:

    wishMe()
    query = takeCommand()
    
    #Logic
    
    if query:
        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")
    

    【讨论】:

    • 谢谢,但我该如何解决麦克风无法接收输入的问题?
    【解决方案2】:

    你的函数没有返回任何东西。比如:

    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...")
        return query 
    

    如果对你有帮助,别忘了标记我的答案已接受

    【讨论】:

    • 你需要像我提到的那样在你的函数中添加返回查询,并在你的逻辑中添加 "if query:" ,就像 Mayank Porwal 所说的那样
    【解决方案3】:

    我想帮助你,但我没有你想要做什么的背景,但我会给你一个例子:

    wishMe()
    query = takeCommand()
    
    #Logic
    
    if query:
    # an then you can check your condition
    query.lower()
    

    【讨论】:

    • 感谢您的努力,但该错误已被前一位成员纠正。但是,如果您能帮助我,还有一个问题。每次我运行代码时,我的麦克风都不会接收到一个输入,它应该将其转换为语音到文本,作为我集成的语音识别系统的一部分。
    【解决方案4】:

    你需要添加takeCommand函数的返回类型...然后 query=takeCommand 函数可以工作,否则它可以给你一个错误,只是没有类型...... 所以,在 try 和 except 之后在 takeCommand 函数中添加 return query

    希望对你有帮助

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 2019-12-04
      • 2012-08-09
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多