我正在学校休息期间,我正在建立一个个人助理机器人作为挑战。它以前没有问题,但我开始得到TypeErrors,尽管我能够检查“语音”是否以字符串形式返回。我知道这是因为TypeError会在经过while循环几次之后随机弹出(所以“语音”必须不会以字符串形式返回...)
我在TypeErrors上找到了其他页面,其他内容返回None和NonTypes,如one和two,但似乎都没有帮助我解决困境。我还浏览了无数其他页面,试图寻找有用的东西,但无济于事。
错误始终显示第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会更频繁出现,但我不明白为什么。我第一次注意到这个错误,当我有一个Chatbot库对话机器人在没有发出命令时接管,并且当我从我的代码中删除时错误会停止,但是这没有解决它。任何帮助修复它并向我解释原因将不胜感激。
如果您需要我的额外文件(Messanger_Alert,Weather等),我愿意提供该代码,如果它可能有所帮助。
感谢您的帮助!
答案 0 :(得分:1)
使用if any(word in speech for word in ("Jason", "jason")):
,Python正在尝试检查word
是否在speech
中。您从speech
获得return_audio()
。当return_audio()
遇到任何指定的异常时,它不会执行任何显式return
语句,因此会隐式返回None
,而不能在搜索任何内容时进行迭代。< / p>
要解决此问题,只需在尝试查看结果之前检查该函数是否产生了真实结果。
while True:
speech = return_audio()
if not speech:
continue