我正在用Python编写AI程序,但是遇到了一个问题。我希望当问题中包含关键词KeyS时,程序能输出答案。运行时出错的代码行是:
if you == KeyS: robot = 'hi friend'
我尝试过的代码如下:
import timeimport pyttsx3day = time.asctime(time.localtime(time.time()))KeyS = 'hi', 'hello'loop = Truewhile 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是一个字符串元组。