Chatbot in python

Chatbot in python

Introduction

A chatbot in Python is a software application built with the Python programming language that simulates human-like conversation via text.

Python is suited for this project development for several reasons:

  • Ease of Use
  • Extensive Libraries
  • AI and Machine Learning Support
  • Cross-Platform Compatibility
  • Community Support
chatbot

Types of chatbots

While talking about chatbots, let’s understand different types of chatbots too. There are three types of chatbots respectively.

Rule-based chatbots


AI-powered chatbots


Hybrid chatbots


Explanation of the code

This is how our code and its components work. You can customize this code and add more questions if you want the chatbot to be more interactive and engaging. By expanding the question list and improving logic, you can make the chatbot feel more intelligent and personal.

Importing Libraries

import time
import random
  • random: Used to select random responses or choices from lists.

Greeting the User

name = input("Hello, what is your name? ")
time.sleep(2)
print("Hello " + name)
  • Asks for the user’s name.
  • Waits 2 seconds before greeting back using the entered name.

Asking About Feelings

feeling = input("How are you today? ")
time.sleep(1)
if "good" in feeling:
    print("I'm feeling good too!")
else:
    print("I'm sorry to hear that!")
  • Takes user input for their mood.
  • If the word “good” is in the input, the chatbot responds positively. Otherwise, it gives a sympathetic message.

Favorite Colour

favcolour = input("What is your favourite colour? ")

colours = ["Red","Green","Blue"]
time.sleep(1)
print("My favourite colour is " + random.choice(colours))
  • Asks for the user’s favorite color.
  • The chatbot then randomly picks one color from a list and says it’s its favorite.

Asking About Activities

mood = input("What are you doing today? ")
mood = ["dancing", "reading", "playing", "watching TV", "hiking"]
time.sleep(1)
print("I feel like " + random.choice(mood) + " today")
  • Asks what the user is doing, but then overwrites the variable mood with a list of activities.
  • Picks a random one and says it feels like doing that.
    (Note: overwriting the variable removes the user’s input — this is a logical mistake.)

Asking About Love Life

love=input("Do you have a girlfriend? ")
love = ["Yes", "No"]
time.sleep(1)
input("cool")
  • Asks a personal question, then reassigns love to a list.
  • Uses input("cool") which doesn’t store input — it just pauses until the user presses Enter.
    (Again, a logical error: user input is ignored.)

Favorite Food

food = input("Which food type do you like most? ")
food=["Asian", "Japanese", "Italian", "Chinese", "Latin"]
time.sleep(1)
print("I mostly like " + random.choice(food))
  • Asks about food preferences, then overwrites food variable with a list and picks one randomly.

Favorite Sport

sport = input("Which sport do you love to watch the most? ")
sport=["cirket", "Baseball", "Soccer", "Rugby", "Athletics"]
time.sleep(1)
print("I love to watch " + random.choice(sport))
  • Similar structure: asks the user, ignores their answer, and prints a random favorite sport.
  • “cirket” is likely a typo for “cricket.”

Fun Facts and Trivia

peak = input("Do you know the height of world's tallest mountain? ")
time.sleep(1)
print("8848")
print("Anyone can answer that.")
  • A quick quiz where the bot ignores the user’s answer and prints the height of Mount Everest.
planet = input("How many planet are there in this solar system")
time.sleep(1)
print("I guess there are 8 planets")
  • Prints the correct fact regardless of the user’s input.

Horoscope and Exercise

horscope = input("What is the name of your horscope? ")
horscope=["Leo", "Cancer", "Capricon", "Pisces", "Virgo"]
time.sleep(1)
print("My horscope name is " + random.choice(horscope))
  • Asks for user’s horoscope but overwrites it, then picks a random one.
    (Also contains a typo: “Capricon” → “Capricorn”)
exe = input("How many sit up can you do? ")
exe=["15", "34", "25", "40", "50"]
time.sleep(1)
print("I can do around " + random.choice(exe))
  • Asks how many sit-ups the user can do and responds with a random number.

Goodbye Message

tired = input("I am feeling tired today.")
time.sleep(1)
print("See you! Have a nice day.")
  • Ends the chat politely after the final statement.

Benefits of chatbots

  • 24/7 Availability: Chatbots work round-the-clock without downtime.
  • Cost Efficiency: Reduces the need for large customer service teams.
  • Scalability: Easily handles thousands of users simultaneously.
  • Personalization: Uses stored data to deliver customized responses.
  • Increased Engagement: Keeps users active with quick, interactive replies.
  • Integration Capability: Works seamlessly with websites, apps, and CRMs.
  • Data Collection: Gathers valuable customer insights for analytics and improvement.

How to use this chatbot project?

  • Install python version 3.
  • Set up an editor or IDE. (vs code, pycharm, anaconda)
  • Copy the project and paste on the editor.
  • Execute the program.
  • Start a conversation with the bot.
  • Enjoy and Share!

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *