def talk(text):
engine.say(text)
engine.runAndWait()
listener = sr.Recognizer()
engine = pyttsx3.init()
def take_command():
try:
with sr.Microphone() as source:
talk('listening')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
command = command.replace('alexa', '')
talk(command)
except:
pass
def run_alexa():
command = take_command()
print(command)
if 'play' in command:
song = command.replace('play','')
pywhatkit.playonyt(song)
talk('playing' + song)
elif 'time' in command:
time = datetime.datetime.now().strftime('%H %M %p')
talk('time is' + time)
elif 'search' in command:
person = command.replace('search','')
info = wikipedia.summary(person, 2)
talk(info)
print(info)
elif 'joke' in command:
talk(pyjokes.get_joke())
elif 'google' in command:
google = command.replace('google','')
pywhatkit.search(google)
talk(google)
else:
talk('say that again please')
我试图通过指定它是一个字符串来更改,但我失败了我正在运行这个,当我询问是否在响应中找到STR时,它告诉我STR是一个非类型,我不确定它是这样的,因为引号还是因为它在其他地方指定
如果你运行这个:
command = take_command()
print(command)
命令必须是None因为你的函数(take_command)不返回任何东西,所以默认返回None。