【问题标题】:how do you make a parameter of a function into a string如何将函数的参数变成字符串
【发布时间】:2019-09-08 01:48:24
【问题描述】:

我正在制作一个聊天机器人,我正在尝试创建一个函数来获取问题并以某种方式做出响应,但我收到一条错误消息,上面写着 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")

【问题讨论】:

  • 您已经将字符串传递给函数,如果您必须将对象转换为字符串,您应该使用str - str(question)

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2017-01-29
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多