简体   繁体   English

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

[英]how do you make a parameter of a function into a string

I am making a chatbot and I am trying to create a function that gets a question and responds in a certain way but I receive an error that says TypeError: argument of type 'function' is not iterable regarding the if statement in the "def quanda" section how can I solve this issue? 我正在创建一个聊天机器人,并且试图创建一个能够获取问题并以某种方式响应的函数,但是我收到一条错误,提示您输入TypeError: argument of type 'function' is not iterable对于“ 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")

Thats because get_audio is a method, you need to call it with () . 那就是因为get_audio是一种方法,所以您需要使用()进行调用。 So, whereever you care calling get_audio method call it like this: get_audio() . 因此,无论您在get_audio调用get_audio方法, get_audio()像这样调用它: get_audio()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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