首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python-3.x if / elif中的语音操作

Python-3.x if / elif中的语音操作
EN

Stack Overflow用户
提问于 2021-11-19 22:49:47
回答 1查看 39关注 0票数 0

我从Python的小事做起。就其本身而言,我正在组装一个类似于Alexa,Jarvis,Cortana的应用程序,以及其他通过语音自动执行操作的应用程序。

Python版本: Python 3.10.0

我正在使用这些库:

代码语言:javascript
复制
import speech_recognition as sr
import pyttsx3
import pywhatkit
import urllib.request
import json
import datetime
import wikipedia

我的问题是,当我告诉"BOT“或应用程序向"x”人发送消息时,它没有识别出它应该做的操作,所以我把代码留在了这里。

代码语言:javascript
复制
    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)

变量momdad的编号分别为。动作pywhatkit.sendwhatmsg_instantly通过WhatsApp自动发送消息。动作msg = rec.replace ('message to xx', '')用空消息替换了本例中的初始文本"message to mom“,这样机器人就不会重复该消息。只发送在变量msg之后所说的内容一个示例:"Message to mom“-> "Hello mom”<-这将发送机器人

如果问题是当我说"Bot将消息发送到xx或xx“时,它无法识别我。

我很感谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70041735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档