簡體   English   中英

錯誤:TypeError:只能將 str(不是“NoneType”)連接到 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() function 中寫入return voice_data 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 字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM