Как выбрать только ключевое слово? - PullRequest
0 голосов
/ 19 июня 2020

Я пытаюсь вывести конкретный c ответ на основе ввода пользователя, теперь код работает, но только для однословных ответов. Я пытаюсь добиться того же результата, но пользователь должен ввести полное предложение, содержащее назначенные слова. Имейте в виду, что я должен использовать функцию split().

Мой код:

def welcome():
    print('Welcome to the automated technical support system.')
    print('Please describe your problem.')
# *************************************
def get_input():
  return input().lower()

# *************************************
def main():

    welcome()  
    while True:
      query = get_input() 
      if query == 'crashed':
        print("Are the drivers up to date?")

      elif query == 'blue': 
        print("Ah, the blue screen of death. And then what happened?")

      elif query == 'hacked':
        print("You should consider installing anti-virus software.")

      elif query == 'bluetooth':
        print("Have you tried mouthwash?")

      elif query == 'windows':
        print("Ah, i think i see your problem. What version?")

      elif query == 'apple':
        print("You do mean the computer kind?")

      elif query == 'spam':
        print("You Should see if your mail client can filter")

      elif query == 'connection':
        print("Contact Telkom.")


      elif not query=='quit':
        print('Curious, tell me more.')

      elif query == 'quit':
        break  

main()   
# ************************************* 

Пример вывода:

Welcome to the automated technical support system.
Please describe your problem.

>>crashed
Are the drivers up to date?
>>yes
Curious, tell me more.

Мне нужен результат:

Welcome to the automated technical support system.
Please describe your problem.

>> my pc crashed
Are the drivers up to date?
>>yes
Curious, tell me more.

Ответы [ 3 ]

0 голосов
/ 19 июня 2020

В том месте, где вы используете '==' в своем операторе if, вы можете использовать ключевое слово 'in', чтобы вы могли получить ответ, даже если напишите это: Разбитый, надеюсь, вы понимаете и будете полезны cya.

0 голосов
/ 19 июня 2020

Этого можно добиться, просмотрев ключевое слово в строке query. Есть несколько способов проверить подстроку в python.

if 'crashed' in query:
    # your logic
elif 'blue' in query:
   # your logic
...

Для получения дополнительных сведений о других параметрах вы можете проверить https://stackabuse.com/python-check-if-string-contains-substring/

0 голосов
/ 19 июня 2020

Это может быть то, что вы ищете:

if 'crashed' in query:
    # Do stuff
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...