Python 错误:TypeError:只能将str(而不是“NoneType”)连接到str
我正在用Python3.8做一个语音助手,我使用一个地图url来创建一个行程。 问题是我有一个错误,我不知道如何修复它: 这是我的密码Python 错误:TypeError:只能将str(而不是“NoneType”)连接到str,python,google-maps,typeerror,python-webbrowser,Python,Google Maps,Typeerror,Python Webbrowser,我正在用Python3.8做一个语音助手,我使用一个地图url来创建一个行程。 问题是我有一个错误,我不知道如何修复它: 这是我的密码 def RecordAudio(ask = False): print("micro on") voice_data = '' r = sr.Recognizer() with sr.Microphone() as source: r.adjust_for_ambient_noise(source)
def RecordAudio(ask = False):
print("micro on")
voice_data = ''
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
if ask:
print(ask)
audio = r.listen(source)
try:
voice_data = r.recognize_google(audio, language="en-EN")
print(voice_data)
respond(voice_data)
except sr.UnknownValueError:
if "" in voice_data.lower():
RecordAudio()
else:
print("Sorry, I did not understand, could you repeat ?")
RecordAudio()
except sr.RequestError:
print("I can't answer for the moment")
def respond(voice_data):
if "itineraire" in voice_data:
frommaps = RecordAudio('Where do you go from?')
tomaps = RecordAudio("Where do you go to?")
print("Calculating...")
time.sleep(1)
itineraireurl = "https://www.google.fr/maps/dir/" +frommaps +'/'+tomaps
webbrowser.get().open(itineraireurl)
错误呢
File "main.py", line 86, in respond
itineraireurl = "https://www.google.fr/maps/dir/" +frommaps +'/'+tomaps
TypeError: can only concatenate str (not "NoneType") to str
我只需要在我的
record\u audio()
函数中编写return voice\u data
。检查你的变量from和to是否为空/ie未定义或类似的东西不,代码中没有问题,变量都已定义,这就是我不明白的原因。哦,该死,这是我的错,你试图解决的问题,它是什么类型的??在python编译器中,键入(from)并告诉我它是什么,很可能我怀疑您试图将非字符串变量转换为字符串变量?可以定义一个变量,并且仍然具有值none。我只是更改代码,我没有看到“from”已经用于python。我把“from”改为“frommaps”