0

Hi I am new to python and I am wondering if it is possible to have the same response for two different inputs?

import re

# Define a list of keywords and their associated responses.
keywords_to_responses = {
    'hello' and 'hi': 'Hi there! How can I help you?',
    'goodbye': 'Goodbye!',
    'thank you': 'You\'re welcome! Let me know if you need any more help.',
    'how are you': 'I\'m just a chatbot, but I\'m here to help! How can I assist you today?',
}

def find_matching_response(user_input):
    for keyword, response in keywords_to_responses.items():
        if re.search(r'\b' + keyword + r'\b', user_input):
            return response
    return 'I\'m sorry, I don\'t understand. Can you please rephrase your question?'

def main():
    print('Welcome to the Chatbot! (Type "quit" to exit)')

    while True:
        user_input = input('You: ').lower()

        if user_input == 'quit':
            break

        response = find_matching_response(user_input)
        print('Chatbot:', response)

if __name__ == '__main__':
    main()

I was expecting it to give the same reponse for hello and hi, but it only gave the reponse for hi. how do i change this?

  • If you check what `"hello" and "hi"` evaluates to, you'll see it evaluates to `"hi"`, because you're getting the last value of the operator because `"hello"` is `True` to the `and`. – Skenvy May 13 '23 at 07:18

2 Answers2

3

Add a separate entry for hello and hi. That way, both words (keys in your dictionary) will map to the same sentence (values in your dictionary).

# Define a list of keywords and their associated responses.
keywords_to_responses = {
    'hello': 'Hi there! How can I help you?',
    'hi': 'Hi there! How can I help you?',
    'goodbye': 'Goodbye!',
    'thank you': 'You\'re welcome! Let me know if you need any more help.',
    'how are you': 'I\'m just a chatbot, but I\'m here to help! How can I assist you today?',
}
Naum Raskind
  • 174
  • 4
  • I am going to make a relatively big dictionary, and I want to make it more compact so i want a more organised input and outputs. Is there a way to make it happen? – Ryan Cheung May 13 '23 at 07:20
  • 1
    There's a post I found that talks about how to assign one value to multiple keys at once. Hopefully you find it helpful! https://stackoverflow.com/questions/2974022/is-it-possible-to-assign-the-same-value-to-multiple-keys-in-a-dict-object-at-onc – Naum Raskind May 13 '23 at 07:24
1

The and in your dictionary is not doing what you think you are. The simplest correct syntax is

keywords_to_responses = {
    'hello': 'Hi there! How can I help you?',
    'hi': 'Hi there! How can I help you?',
    'goodbye': 'Goodbye!',
    'thank you': 'You\'re welcome! Let me know if you need any more help.',
    'how are you': 'I\'m just a chatbot, but I\'m here to help! How can I assist you today?',
}

Obviously this is not optimal because you're repeating yourself. You might consider assigning each of the response strings to their own variables and then mapping inputs to these variables. Enums are handy for this.

from enum import Enum
class Response(Enum):
  HI = 'Hi there! How can I help you?'
  BYE = 'Goodbye!'
  YOURE_WELCOME = 'You\'re welcome! Let me know if you need any more help.'
  JUST_BOT = 'I\'m just a chatbot, but I\'m here to help! How can I assist you today?'

keywords_to_responses = {
    'hello': Response.HI,
    'hi': Response.HI,
    'goodbye': Response.BYE,
    'thank you': Response.YOURE_WELCOME,
    'how are you': Response.JUST_BOT,
}

Maybe even consider using the above, but mapping from a response type to an array of inputs. Good luck with Python!

this-sam
  • 182
  • 1
  • 14