我从Python的小事做起。就其本身而言,我正在组装一个类似于Alexa,Jarvis,Cortana的应用程序,以及其他通过语音自动执行操作的应用程序。
Python版本: Python 3.10.0
我正在使用这些库:
import speech_recognition as sr
import pyttsx3
import pywhatkit
import urllib.request
import json
import datetime
import wikipedia我的问题是,当我告诉"BOT“或应用程序向"x”人发送消息时,它没有识别出它应该做的操作,所以我把代码留在了这里。
elif 'send message to ' in rec:
if 'dad' in rec == True:
msj = rec.replace('message to dad', '')
talk('Sending message to dad...' + msj)
print(dad, msj)
pywhatkit.sendwhatmsg_instantly(dad, msj, 20)
elif 'mom' in rec == True:
msj = rec.replace('message to mom', '')
talk('Sending message to mom...' + msj)
print(mom, msj)
pywhatkit.sendwhatmsg_instantly(mom, msj, 20)变量mom和dad的编号分别为。动作pywhatkit.sendwhatmsg_instantly通过WhatsApp自动发送消息。动作msg = rec.replace ('message to xx', '')用空消息替换了本例中的初始文本"message to mom“,这样机器人就不会重复该消息。只发送在变量msg之后所说的内容一个示例:"Message to mom“-> "Hello mom”<-这将发送机器人
如果问题是当我说"Bot将消息发送到xx或xx“时,它无法识别我。
我很感谢你的帮助。
发布于 2021-11-19 22:53:55
从您的条件中删除== True。
Python比较链,所以'dad' in rec == True意味着('dad' in rec) and (rec == True)。这种情况在你的程序中永远不会成功。
只需使用if 'dad' in rec:和elif 'mom' in rec:即可。
请参阅https://docs.python.org/3/reference/expressions.html#comparisons
https://stackoverflow.com/questions/70041735
复制相似问题