简体   繁体   中英

Error: TypeError: can only concatenate str (not "NoneType") to str

I'm doing a vocal assistant in Python 3.8 and I use a maps url to create an itinerary. The problem is that I have an error and I have no idea how to fix it: this is my code

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)

And the error

  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

I just had to write return voice_data in my record_audio() function.

itineraireurl = "https://www.google.fr/maps/dir/" +frommaps +'/'+tomaps
    webbrowser.get().open(itineraireurl)

Put above line of code in form of string like below:

itineraireurl = f"https://www.google.fr/maps/dir/ {frommaps} +'/'+tomaps.webbrowser.get().open(itineraireurl)"

PS: Try to use python f string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM