0

我正在制作一个聊天机器人,我正在尝试创建一个获取问题并以某种方式响应的函数,但我收到一个错误,指出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")
4

1 回答 1

2

那是因为get_audio是一个方法,你需要用(). 因此,无论您在哪里调用get_audio方法,都可以像这样调用它:get_audio()

于 2019-09-08T06:40:11.637 回答