0% found this document useful (0 votes)
9 views2 pages

Chatbot

This document provides code to create a simple chatbot program that can hold basic conversations by responding to common greetings and parting phrases with predefined responses as well as unknown inputs. The program uses a dictionary to store potential responses and selects randomly from the options.

Uploaded by

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

Chatbot

This document provides code to create a simple chatbot program that can hold basic conversations by responding to common greetings and parting phrases with predefined responses as well as unknown inputs. The program uses a dictionary to store potential responses and selects randomly from the options.

Uploaded by

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

Program to create a simple chatbot

import random

# Define responses for the chatbot

responses = {

"hi": ["Hello!", "Hi there!", "Hey!"],

"how are you": ["I'm good, thanks for asking!", "I'm doing well, how about you?"],

"bye": ["Goodbye!", "See you later!", "Bye! Take care!"],

"default": ["I'm not sure what you mean...", "Could you please rephrase that?", "Sorry, I didn't
understand."],

# Function to generate a response to a user input

def generate_response(user_input):

user_input = user_input.lower() # Convert input to lowercase

if user_input in responses:

return random.choice(responses[user_input])

else:

return random.choice(responses["default"])

# Main function to run the chatbot

def main():

print("Chatbot: Hello! How can I help you today?")

while True:

user_input = input("You: ")

if user_input.lower() == 'exit':
print("Chatbot: Goodbye!")

break

response = generate_response(user_input)

print("Chatbot:", response)

# Run the chatbot

if __name__ == "__main__":

main()

You might also like