我正在制作一个聊天机器人,我试图创建一个函数来获取一个问题并以某种方式进行响应,但是我收到一个错误,关于“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")
这是因为
get_audio
是一个方法,所以需要用()
调用它。所以,不管您在哪里调用get_audio
方法,都可以这样调用:get_audio()
。你知道吗相关问题 更多 >
编程相关推荐