Python 如何将函数的参数转换为字符串
我正在制作一个聊天机器人,我试图创建一个函数来获取问题并以某种方式进行响应,但我收到一个错误,上面写着Python 如何将函数的参数转换为字符串,python,python-3.x,speech-recognition,google-text-to-speech,gtts,Python,Python 3.x,Speech Recognition,Google Text To Speech,Gtts,我正在制作一个聊天机器人,我试图创建一个函数来获取问题并以某种方式进行响应,但我收到一个错误,上面写着TypeError:type'function'的参数是不合适的关于“def quanda”部分中的if语句,我如何解决这个问题 import os import speech_recognition as sr from gtts import gTTS import playsound import time def speak(text): tts = gTTS(text=te
TypeError:type'function'的参数是不合适的
关于“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")
这是因为
get\u audio
是一种方法,您需要使用()
调用它。因此,无论您在哪里调用get\u audio
方法,都可以这样调用它:get\u audio()
这是因为get\u audio
是一个方法,您需要使用()
调用它。因此,无论您在何处调用get\u audio
方法,都可以这样调用:get\u audio()
您已经在向函数传递字符串,如果必须将对象转换为字符串,则应使用str
-str(问题)
您已经向函数传递字符串的可能重复项,如果必须将对象转换为字符串,则应使用str
-str(问题)
的可能重复项