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?
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?',
}