36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
#Let's Create a Simple Chatbot in Python
|
|
#define a greet function
|
|
def greet(bot_name, birth_year):
|
|
#print the question answers
|
|
print(f"Chatbot: Hello, I am {bot_name}. I was created in {birth_year}.")
|
|
print("Chatbot: How can I help you today?")
|
|
|
|
#Use another Function respond
|
|
def respond(user_input):
|
|
#use conditional statements
|
|
if user_input.lower() == "hi" or user_input.lower() == "hello":
|
|
return "Hello there! How can I help you today?"
|
|
elif user_input.lower() == "bye":
|
|
return "Goodbye! Have a great day."
|
|
elif user_input.lower() == "what is your good name?":
|
|
return "My name is Chatbot."
|
|
elif user_input.lower() == "what is your birth year?":
|
|
return "I was created in 2021."
|
|
elif user_input.lower() == "what can you do?":
|
|
return "I can answer your questions, have a conversation with you, and perform basic tasks."
|
|
elif "weather" in user_input.lower():
|
|
return "I am not able to check the weather at the moment, sorry."
|
|
else:
|
|
return "I'm sorry, I don't understand what you're trying to say. Could you please rephrase that?"
|
|
|
|
bot_name = "Chatbot"
|
|
birth_year = 2021
|
|
greet(bot_name, birth_year)
|
|
|
|
while True:
|
|
user_input = input("You: ")
|
|
response = respond(user_input)
|
|
print(f"Chatbot: {response}")
|
|
if user_input.lower() == "bye":
|
|
break
|