Error: TypeError: can only concatenate str (not "NoneType") to str
Error: TypeError: can only concatenate str (not "NoneType") to str
我在 Python 3.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)
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_audio()
函数中写入 return voice_data
。
itineraireurl = "https://www.google.fr/maps/dir/" +frommaps +'/'+tomaps
webbrowser.get().open(itineraireurl)
将上面的代码行以字符串的形式放在下面:
itineraireurl = f"https://www.google.fr/maps/dir/ {frommaps} +'/'+tomaps.webbrowser.get().open(itineraireurl)"
PS : 尝试使用 python f string.
我在 Python 3.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)
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_audio()
函数中写入 return voice_data
。
itineraireurl = "https://www.google.fr/maps/dir/" +frommaps +'/'+tomaps
webbrowser.get().open(itineraireurl)
将上面的代码行以字符串的形式放在下面:
itineraireurl = f"https://www.google.fr/maps/dir/ {frommaps} +'/'+tomaps.webbrowser.get().open(itineraireurl)"
PS : 尝试使用 python f string.