Certainly! Here's an example of a simple chatbot project in Python:
Source Code Example
import random
# Function to greet the user
def greet():
responses = ["Hello!", "Hi there!", "Greetings!", "Nice to meet you!"]
return random.choice(responses)
# Function to handle user's messages
def respond(message):
greetings = ["hi", "hello", "hey", "greetings"]
if message.lower() in greetings:
return greet()
else:
return "I'm sorry, I didn't understand that."
# Main loop
while True:
user_message = input("You: ")
bot_response = respond(user_message)
print("Bot: " + bot_response)
In this example, the chatbot recognizes a few simple greetings and responds accordingly. If the user's message matches one of the greetings, the chatbot will randomly select a response from a list of possible responses. Otherwise, it will provide a default "I'm sorry, I didn't understand that" response.
To run this code, save it to a file (e.g., `chatbot.py`) and run it using a Python interpreter. You can then interact with the chatbot by typing messages and pressing Enter.
Feel free to expand on this code and add more functionality based on your requirements.