Learn to Code via Tutorials on Repl.it

← Back to all posts
5
Simple Chat Bot [Beginner Level, Python]
ChristianThone (34)

What is a chatbot?

A chat bot can be defined in many ways, I define it as a program designed and developed to keep the user entertained. Mayhap you define it differently that doesn't matter, what matters is that we are gonna build a simple one today; Before we start, I will be assuming that you have a basic understanding of Python 3.0. If not then I think you should learn it before reading the rest of this tutorial or you will get confused.

How do we start building a chatbot?

Simple! We need some variables to store some information. Please define the following(in main.py):

UserInput = ""
ChatBot_Name = "Ozzi"

There, now we have a few variables and two lines of code!!!

Let's not get too excited though, we still have quite a bit to do. Feel free to name your chatbot whatever you would like, I have named mine "Ozzi". Now for the fun part, collecting User Input to reference later. Write the following into main.py:

UserInput = str.lower(input(": "))

This will store the user input to our variable, UserInput. We will refrence this soon. You can see that I wrote

str.lower()

this will make the User Input into all lower case letters, that way we don't have to check for all possible combinations of UPPER CASE and lowercase letters. Now we want the chat bot to talk dont we??? YES! So let's give it a small brain so it can answer the user when the input "Hi". So in main.py write the following:

if UserInput == "hi":
 print(ChatBot_Name + ": Hello!")

As you can see here we check if the user says "hi", if this is python True then we will have the Chat Bot output "Hello". I'll show you an example.

As you can see I said hi to my chat bot and it responded!!!

Now that is a simple thing, lets have it answer a question. Let's ask it something simple. "Who made you?", great question so lets program it like the other part.

elif UserInput == "who made you?":
 print(ChatBot_Name + ": I was made by Christian Thone")
# You can change my name to your name.

Here is the output:

There we go now we have 2 Inputs, that the Chat Bot can can respond too, if you use the same method for all the other new inputsyou wish for your bot to have, you will have a good chat bot that people will love. Stay tuned for
"A Slightly More Advanced ChatBot [Intermediate, Python]", where I will go over how to make a more advanced chat bot, with more variables and better responses.

Keep coding,
Christian Thone
@ChristianThone

Commentshotnewtop
1
PYer (2045)

In A Slightly More Advanced ChatBot [Intermediate, Python], make it so that it scans messages for keywords instead.

2
ChristianThone (34)

@PYer Hey yea, that is gonna be for my "An Advanced ChatBot [Advanced, Python]", I'm writing A Slightly More Advanced ChatBot [Intermediate, Python] right now!

1
ChristianThone (34)

@PYer Alright A Slightly More Advanced ChatBot [Intermediate, Python] is out now! Go check it out here. And please let me know if you want more tutorials just like this one, I'll be glad to know what you want to learn about!