|
| 1 | +import random |
| 2 | + |
| 3 | +# Define responses for the chatbot |
| 4 | +responses = { |
| 5 | + "hello": ["Hello!", "Hi there!", "Hey!"], |
| 6 | + "how are you": ["I'm good, thank you!", "I'm doing well, thanks for asking.", "All good here!"], |
| 7 | + "what's your name": ["I'm just a simple chatbot.", "I don't really have a name, I'm just here to help."], |
| 8 | + "bye": ["Goodbye!", "See you later!", "Bye! Take care!"], |
| 9 | + "hi": ["Hi!","Hey!"], |
| 10 | + |
| 11 | + |
| 12 | +} |
| 13 | + |
| 14 | +def get_response(message): |
| 15 | + # Convert the message to lowercase for easier matching |
| 16 | + message = message.lower() |
| 17 | + |
| 18 | + # Check if the message matches any predefined responses |
| 19 | + for key in responses: |
| 20 | + if message in key: |
| 21 | + return random.choice(responses[key]) |
| 22 | + |
| 23 | + # If no predefined response is found, return a default response |
| 24 | + return "I'm sorry, I didn't understand that." |
| 25 | + |
| 26 | +def main(): |
| 27 | + print("Welcome to the Simple Chatbot!") |
| 28 | + print("You can start chatting. Type 'bye' to exit.") |
| 29 | + |
| 30 | + while True: |
| 31 | + # Get user input |
| 32 | + user_input = input("You: ") |
| 33 | + |
| 34 | + # Exit the loop if the user types 'bye' |
| 35 | + if user_input.lower() == "bye": |
| 36 | + print("Chatbot: Goodbye! Have a great day!") |
| 37 | + break |
| 38 | + |
| 39 | + # Get and print the chatbot's response |
| 40 | + response = get_response(user_input) |
| 41 | + print("Chatbot:", response) |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + main() |
0 commit comments