gpt4 book ai didi

python - 如何在输入中搜索并打印项目,python?

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:08 26 4
gpt4 key购买 nike

我刚刚开始编码并有一个疑问。我正在为一个名为 Sasha 的聊天机器人编写一个脚本,但是我找不到任何方法来解决句子中并非所有单词都匹配的问题。比如说,我想要求它以不同的方式检查日期,而不仅仅是说“日期”。我该怎么做呢?如有任何帮助,我们将不胜感激。

Database =[

['hello sasha', 'hey there'],

['what is the date today', 'it is the 13th of October 2017'],

['name', 'my name is sasha'],

['weather', 'it is always sunny At Essex'],

]

while 1:
variable = input("> ")

for i in range(4):
if Database[i][0] == variable:
print (Database[i][1])

最佳答案

您可以使用“in”来检查列表中是否有某些内容,如下所示:(伪代码)

list = ['the date is blah', 'the time is blah']

chat = input('What would you like to talk about')

if chat in ['date', 'what is the date', 'tell the date']:
print(list[0])

elif chat in ['time', 'tell the time']:
print(list[1])

etc.

你应该考虑了解什么是字典,这会对你有很大帮助。

关于python - 如何在输入中搜索并打印项目,python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46690604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com