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

Chatbot

The document contains a Python script that imports the random library and defines a dictionary of predefined responses for a chatbot. It includes a function to generate responses based on user input keywords such as greetings, farewells, and thanks. The script runs an interaction loop where it continuously prompts the user for input and responds accordingly.

Uploaded by

ayushranjan1923
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)
9 views2 pages

Chatbot

The document contains a Python script that imports the random library and defines a dictionary of predefined responses for a chatbot. It includes a function to generate responses based on user input keywords such as greetings, farewells, and thanks. The script runs an interaction loop where it continuously prompts the user for input and responds accordingly.

Uploaded by

ayushranjan1923
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/ 2

# Importing required libraries

import random

# Dictionary of predefined responses

responses = {

"greeting": ["Hello!", "Hi!", "Welcome!"],

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

"thanks": ["You're welcome!", "No problem!", "My pleasure!"],

"default": ["I'm sorry, I didn't understand.", "Could you please rephrase"]

# Function to generate a response based on user input

def generate_response(user_input):

user_input = user_input.lower()

# Check for common keywords in user input

if "hello" in user_input or "hi" in user_input:

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

elif "goodbye" in user_input or "bye" in user_input:

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

elif "thank" in user_input:

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

else:

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

# Main interaction loop

while True:

# Get user input

user_input = input("User: ")

# Generate and display bot response

bot_response = generate_response(user_input)

print("Bot:", bot_response)
output -

You might also like