Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换字符串的Python TypeError为None_Python_Python 2.7_Error Handling_Typeerror - Fatal编程技术网

转换字符串的Python TypeError为None

转换字符串的Python TypeError为None,python,python-2.7,error-handling,typeerror,Python,Python 2.7,Error Handling,Typeerror,我目前正在建设一个个人助理机器人作为一个挑战,而我在学校休息。它以前没有问题,但我开始出现打字错误,尽管我能够检查“speech”是否以字符串形式返回。我之所以知道这一点,是因为在while-True循环中运行几次之后,TypeError会随机弹出(因此“speech”不能作为字符串返回…) 我还发现了其他关于类型错误和返回无和非类型(如and)的页面,但它们似乎都无助于我的困境。我还浏览了无数其他页面,试图找到一些有用的东西,但都没有用 该错误始终显示第19行: import speech_r

我目前正在建设一个个人助理机器人作为一个挑战,而我在学校休息。它以前没有问题,但我开始出现打字错误,尽管我能够检查“speech”是否以字符串形式返回。我之所以知道这一点,是因为在while-True循环中运行几次之后,TypeError会随机弹出(因此“speech”不能作为字符串返回…)

我还发现了其他关于类型错误和返回无和非类型(如and)的页面,但它们似乎都无助于我的困境。我还浏览了无数其他页面,试图找到一些有用的东西,但都没有用

该错误始终显示第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
回溯(最近一次呼叫最后一次):
文件“C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py”,第25行,在
启动()
文件“C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py”,第14行,在启动中
声音()
文件“C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py”,第20行,语音
听,开始
文件“C:\Users\lukec\PycharmProjects\Sysgen\u AI\Listen.py”,第19行,开始
如有(以“Jason”、“Jason”一词代替“Jason”):
文件“C:\Users\lukec\PycharmProjects\Sysgen\u AI\Listen.py”,第19行,在
如有(以“Jason”、“Jason”一词代替“Jason”):
TypeError:类型为“NoneType”的参数不可编辑
我注意到,当听到无法用文字区分的奇怪声音时,打字错误会更频繁地出现,但我不明白为什么。我第一次注意到这个错误是在没有发出命令的情况下让聊天机器人库会话机器人接管时,我想当我从代码中删除它时,错误会停止,但这并不能解决它。如果您能帮我修理并解释原因,我将不胜感激

如果您需要我的额外文件(Messanger_Alert、Weather等),如果可能的话,我愿意提供这些代码

谢谢你的帮助

使用
如果有(word-in-speech-for-word-in(“Jason”,“Jason”):
,Python正在尝试检查
word
是否在
speech
中。您可以从
return\u audio()
获得
speech
。当
return\u audio()
遇到任何指定的异常时,它不会执行任何显式
return
语句,因此隐式返回
None
,在搜索任何内容时无法对其进行迭代

要解决这个问题,只需在尝试查看该结果之前检查该函数是否产生了真正的结果

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