如何将函数的参数转换为字符串

时间:2019-09-08 01:48:24

标签: python python-3.x speech-recognition google-text-to-speech gtts

我正在创建一个聊天机器人,并且试图创建一个能够获取问题并以某种方式响应的函数,但是我收到一条错误消息,内容为TypeError: argument of type 'function' is not iterable,涉及“ def quanda”部分中的if语句我可以解决这个问题吗?

import os
import speech_recognition as sr
from gtts import gTTS
import playsound
import time


def speak(text):
    tts = gTTS(text=text, lang="en")
    filename = "voice.mp3"
    tts.save(filename)
    playsound.playsound(filename)

def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
            print(said)
        except Exception as e:
            print("Exception: " + str(e))

    return said

def qanda(question, response):
    text = get_audio
    if question in text:
        speak(response)
    return response

speak("hello, how can i help you?")

text = get_audio

qanda("hello", "hi there")

quanda("what is the weather", "Its 27 degrees")

1 个答案:

答案 0 :(得分:2)

那是因为get_audio是一种方法,所以您需要使用()来调用它。因此,无论您在哪里调用get_audio方法,都应这样调用它:get_audio()