Categories We Write About

Create a simple chatbot with Python

Creating a simple chatbot in Python can be done using basic libraries without any complex setup. Below is a straightforward example of a rule-based chatbot that responds to user input with predefined answers.

python
# Simple rule-based chatbot in Python def chatbot_response(user_input): user_input = user_input.lower() if "hello" in user_input or "hi" in user_input: return "Hello! How can I help you today?" elif "how are you" in user_input: return "I'm a chatbot, so I'm always good! How about you?" elif "bye" in user_input or "exit" in user_input: return "Goodbye! Have a great day!" elif "your name" in user_input: return "I'm a simple chatbot created in Python." else: return "Sorry, I didn't understand that. Can you please rephrase?" def main(): print("Welcome to the simple Python chatbot! Type 'exit' or 'bye' to end.") while True: user_input = input("You: ") response = chatbot_response(user_input) print("Bot:", response) if user_input.lower() in ['exit', 'bye']: break if __name__ == "__main__": main()

This chatbot checks for keywords in the user’s input and responds accordingly. It runs in a loop until the user types “exit” or “bye”.

If you want, I can also help you build a more advanced chatbot using libraries like nltk or transformers.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About