0% found this document useful (0 votes)
61 views4 pages

Chatbot

The document contains a Python script for a chatbot that responds to various user inputs with predefined responses. It includes functionalities for greeting, providing time, telling jokes, and answering questions about its purpose and capabilities. The chatbot operates in a loop until the user types 'bye' to end the conversation.

Uploaded by

Laxman Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views4 pages

Chatbot

The document contains a Python script for a chatbot that responds to various user inputs with predefined responses. It includes functionalities for greeting, providing time, telling jokes, and answering questions about its purpose and capabilities. The chatbot operates in a loop until the user types 'bye' to end the conversation.

Uploaded by

Laxman Kale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import random

import time
import re
# Define a dictionary for chatbot responsesresponses =
{
"hi": ["Hello! How can I assist you today?", "Hi there! What can I do for you?", "Hello! How's it
going?"],"hello": ["Hi! How's your day going?", "Hello! How can I help you today?", "Hey
there!"],"how are you": ["I'm just a bot, but I'm doing great! How about you?", "I'm functioning
well, thank you! How about you?", "I'm doing awesome! How are you?"],"bye": ["Goodbye!
Have a great day!", "Bye! Take care!", "Goodbye! Stay safe!"],"thank you": ["You're welcome!",
"No problem!", "Glad I could help!"],"name": ["I am a chatbot. I don't have a specific name.",
"You can call me Chatbot!", "I'm your friendly chatbot!"],"what is your name": ["I don't have a
personal name. You can call me Chatbot!", "I'm your assistant bot!"],"time": ["The current time
is: {}", "It's currently: {}"],"joke": ["Why don't skeletons fight each other? They don't have the
guts!","Why did the math book look sad? Because it had too many problems!","Why don't eggs
tell jokes? Because they might crack up!"],"weather": ["It's a sunny day outside! (But you
should check the weather app for accurate data.)","It's a bit cloudy today, but I'm sure it's a
nice day overall!"],"news": ["Breaking News: A new discovery was made in the field of AI
today!","In today's news: Scientists have made a breakthrough in renewable
energy!","Breaking: New technology promises to revolutionize the healthcare industry."],"how
old are you": ["I don't have an age, but I was created to assist you!", "Age doesn't apply to me.
I'm ageless!"],"favorite color": ["I don't have a favorite color, but I think blue is cool!", "I love all
colors equally!"],"favorite food": ["I don't eat, but pizza sounds delicious!", "If I could eat, I'd
love to try sushi!"],"your hobbies": ["I enjoy chatting with people like you!", "My hobby is
assisting people and making conversations fun!"],"what is your purpose": ["My purpose is to
help you by answering your questions and chatting with you.","I am here to assist you and
make your day better!"],"calculation": ["I'm happy to perform some math calculations! Just
type your math expression, and I'll calculate it for you."],"default": ["Sorry, I didn't understand
that. Could you please clarify?", "I'm not sure I understand. Can you try again?"],
}

# Function to get a random response from a list


def get_random_response(key):
if key in responses:
return random.choice(responses[key])
return random.choice(responses["default"])
# Function to get the current time
1
def get_current_time():
return time.strftime("%H:%M:%S", time.localtime())
# Function to evaluate simple math expressions
def calculate_expression(expression):
try:
result = eval(expression)
return f"The result is: {result}"
except:
return "Sorry, I couldn't process that. Please try a valid mathematical expression."
# Function to process user input and generate response
def process_input(user_input):
user_input = user_input.lower().strip() # Normalize input to lowercase and strip extra spaces
# Handling different commands
if "time" in user_input:
return get_random_response("time").format(get_current_time())
elif "joke" in user_input:
return get_random_response("joke")
elif "how are you" in user_input:
return get_random_response("how are you")
elif "hello" in user_input or "hi" in user_input:
return get_random_response("hello")
elif "thank you" in user_input:
return get_random_response("thank you")
elif "bye" in user_input:
return get_random_response("bye")
elif "name" in user_input or "what is your name" in user_input:
return get_random_response("name")
elif "weather" in user_input:
return get_random_response("weather")
elif "news" in user_input:

2
return get_random_response("news")
elif "how old are you" in user_input:
return get_random_response("how old are you")
elif "favorite color" in user_input:
return get_random_response("favorite color")
elif "favorite food" in user_input:
return get_random_response("favorite food")
elif "hobbies" in user_input or "your hobbies" in user_input:
return get_random_response("your hobbies")
elif "purpose" in user_input:
return get_random_response("what is your purpose")
elif re.search(r"[-+/*0-9]", user_input):
# Basic check for mathematical expression
return calculate_expression(user_input)
else:
return get_random_response("default")
# Main function to run the chatbot
def chatbot():
print("Chatbot: Hello! I'm your assistant. Type 'bye' to end the conversation.")
name = input("Chatbot: What's your name? ") # Ask for the user's name
print(f"Chatbot: Nice to meet you, {name}!")
# Loop for continuous conversation until user types 'bye'
while True:
user_input = input(f"{name}: ") # Get input from the user
if user_input.lower() == "bye":
print(f"Chatbot: {get_random_response('bye')}")
break

3
response = process_input(user_input)
print(f"Chatbot: {response}")
# Start the chatbot
chatbot()
OUTPUT:

You might also like