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

2024-09-28 16:58:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个聊天机器人,我试图创建一个函数来获取一个问题并以某种方式进行响应,但是我收到一个错误,关于“def quanda”部分中的if语句TypeError: argument of type 'function' is not iterable,如何解决这个问题?你知道吗

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")

Tags: textimportgetifisresponsedefas