python - 如果 x 属于关键字,AI 会自动回答
问题描述
我正在用 python 对 AI 进行编码,但我有一个问题,如果问题有 KeyS 的关键字,我希望它打印出答案,运行时的错误是:
if you == KeyS:
robot = 'hi friend'
我试过这段代码:
import time
import pyttsx3
day = time.asctime(time.localtime(time.time()))
KeyS = 'hi', 'hello'
loop = True
while loop:
you = input('you:')
if you == KeyS:
robot = 'hi friend'
elif you == 'time':
robot = day
elif you == 'bye':
robot = 'bye sir'
print('robot:' + robot)
robotsay = pyttsx3.init()
robotsay.say(robot)
robotsay.runAndWait()
exit()
else:
robot = 'i do not understand'
print('robot:' + robot)
robotsay = pyttsx3.init()
robotsay.say(robot)
robotsay.runAndWait()
我的英文不好,如有语法错误请见谅
解决方案
你需要改变
if you == KeyS:
robot = 'hi friend'
至
if you in KeyS:
robot = 'hi friend'
因为 KeyS 是一个字符串元组。
推荐阅读
- r - R中的中心窗口(tcl/tk库)
- python-3.x - 如何在 Mac 上使用 sys.stdin.read()
- python - 使用 XlxsWriter 将图表保存为图像 (png/jpg)
- javascript - 根据角度 4 中的某些条件,在子项中单击按钮时从父项中的数组中删除特定组件实例
- java - Java Swing Applet - 运行 appletviewer 时找不到类异常
- python - Flask 测试 - 从蓝图动态测试所有受保护的路由
- ruby-on-rails - 使用 attr_encrypted 使用新密钥加密新数据(覆盖旧数据)
- amazon-web-services - x509:由未知机构使用 AWS IoT 签署的证书
- sql - 删除后表仍然存在,整个数据库也存在
- python - Python signxml XML 签名包。如何为 Signature 标签添加 xml 占位符?