首页 > 解决方案 > 如果 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()

我的英文不好,如有语法错误请见谅

标签: pythonartificial-intelligence

解决方案


你需要改变

  if you == KeyS:
        robot = 'hi friend'

  if you in KeyS:
        robot = 'hi friend'

因为 KeyS 是一个字符串元组。


推荐阅读