【问题标题】:Python TypeError is None for converted stringPython TypeError 对于转换后的字符串为 None
【发布时间】:2017-05-16 07:14:48
【问题描述】:

我目前正在上学休息时构建个人助理机器人作为一项挑战。它以前没有问题,但我开始收到 TypeErrors,尽管我能够检查“speech”是否作为字符串返回。我知道这一点,因为 TypeError 会在运行几次 while True 循环后随机弹出(所以“speech”只能不作为字符串返回......)

我发现了关于 TypeErrors 的其他页面以及返回 None 和 NonTypes 的东西,例如 onetwo,但似乎都没有帮助我的困境。我还浏览了无数其他页面,试图找到有用的东西,但无济于事。

错误总是出现第 19 行:

import speech_recognition as sr
from chatterbot import ChatBot

import Messanger_Alert as MA
import Weather
import Email_Alert
import Chat
import Run


def start():
    #bot = Chat.start()
    MA_client = MA.login()
    print "Hello Luke!"
    print "I am ready for your command! :D"
    while True:
        speech = return_audio()
        try:
            if any(word in speech for word in ("Jason", "jason")): #This is line 19
                if any(word in speech for word in ("Message", "message", "Messenger", "messenger", "Facebook", "facebook")):
                    if any(word in speech for word in ("Send", "send")):
                        MA.send_message(MA_client) 
                        print "Ready for next command!"
                    elif any(word in speech for word in ("Search Friend", "search friend", "Seach friend", "search Friend", "friend", "Friend", "friends", "Friends")):
                        MA.friend_search(MA_client)
                        print "Ready for next command!"
                    elif any(word in speech for word in ("Check", "check")):
                        MA.check_message(MA_client)
                        print "Ready for next command!"
                elif any(word in speech for word in ("Email", "email")):
                    if any(word in speech for word in ("Personal", "personal", "Home", "home")):
                        Email_Alert.login1()
                        print "Ready for next command!"
                    elif any(word in speech for word in ("School", "school", "Dorm", "dorm", "UCSD", "ucsd")):
                        Email_Alert.login2()
                        print "Ready for next command!"
                elif any(word in speech for word in ("Weather", "weather", "Forecast", "forecast")):
                    Weather.decide()
                """else:
                    Chat.talk(bot)
            else:
                Chat.talk(bot)
                """
        except sr.UnknownValueError:
            print "Error! Could not process that audio."
        except sr.RequestError as e:
            print "Error! No internet connection to Google Sound Recognizer."


def return_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
    try:
        speech = r.recognize_google(audio)
        try:
            speech = str(speech)
            print speech
            return speech
        except TypeError:
            print "Error! Could not convert speech to string!"
    except sr.UnknownValueError:
        print "Error! Could not process that audio."
    except sr.RequestError as e:
        print "Error! No internet connection to Google Sound Recognizer."

这是我得到的确切错误:

Traceback (most recent call last):
  File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 25, in <module>
    startup()
  File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 14, in startup
    voice()
  File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 20, in voice
    Listen.start()
  File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Listen.py", line 19, in start
    if any(word in speech for word in ("Jason", "jason")): 
  File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Listen.py", line 19, in <genexpr>
    if any(word in speech for word in ("Jason", "jason")): 
TypeError: argument of type 'NoneType' is not iterable

我注意到当听到无法区分为语言的奇怪噪音时,TypeError 会更频繁地出现,但我不明白为什么。我第一次注意到这个错误是当我有一个聊天机器人库对话机器人在没有发出命令的情况下接管时,我想当我从我的代码中删除它时,错误会停止,但这并没有解决它。任何有关修复它并向我解释原因的帮助将不胜感激。

如果您需要我的额外文件(Messanger_Alert、Weather 等),如果可能有帮助,我愿意提供该代码。

感谢您的帮助!

【问题讨论】:

    标签: python python-2.7 error-handling typeerror


    【解决方案1】:

    对于if any(word in speech for word in ("Jason", "jason")):,Python 会尝试检查word 是否在speech 中。你从return_audio() 得到speech。当return_audio() 遇到任何指定的异常时,它不会执行任何显式的return 语句,因此会隐式返回None,它不能被迭代以搜索任何内容。

    要解决此问题,只需在尝试查看该结果之前检查该函数是否产生了真实结果。

    while True:
        speech = return_audio()
        if not speech:
            continue
    

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 2014-07-11
      • 2010-11-05
      • 2021-08-03
      • 2019-10-10
      • 1970-01-01
      • 2019-10-17
      • 2011-03-16
      • 2011-04-20
      相关资源
      最近更新 更多