【问题标题】:How to search for keywords in the user input python 3如何在用户输入python 3中搜索关键字
【发布时间】:2018-03-20 22:18:17
【问题描述】:

我 15 岁,开始,我正在尝试自学如何编码。

我已经在一个系统上工作了一段时间,并且通过使用其他示例来学习已经取得了一些进展,但是我找不到可以完全学习的指南。

基本上,我想编写一个可以在用户输入中搜索特定关键字的代码。我在其中找到了为这些关键字设置变量的区域,它会找到其中一个在哪里,如果其中一个在,它会打印一个函数。

hi = ['hi', 'hello', 'Hi', 'Hello', 'sup', 'Sup', 'howdy', 'Howdy', 'hey', 'Hey']
bye = ['bye', 'Bye', 'Goodbye', 'goodbye', 'Power Down', 'power down', 'Power Off', 'power off']
shut_down = 0

while shut_down < 1:
  user_input = input("Input: ")
  for element in user_input:
    if element in hi:
      import random
      greeting = random.sample(['Hello there.', 'Howdy partner', 'It is nice to talk to you.', 'Hi friend.', 'Sup mah human!'], 1)
      print (greeting)
    elif element in bye and not hi:
      import random
      farewell = random.sample(['Goodbye friend.', 'Goodbye', 'Farewell', 'See you later', 'Bye bud', 'Thanks for talking with me.'], 1)
      print(farewell)
      shut_down = int(shut_down) + 1

我计划在关键词中添加更多内容,以允许更多响应最终发展对话,但该系统无法正常运行。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    请提供一个更好的例子。 据我了解, 您可以使用关键字列表。

    key_w = ['word1','word2','word3','etc.']
    

    然后要求用户输入并搜索关键字。

    userIn = input('Input: ')
    for element in userIn:
        if element in key_w:
            function_that_does_AI()
    

    然后,根据关键字的时间顺序,函数可以解释它。

    【讨论】:

      猜你喜欢
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多