Skip to content
← Back to Community
A Slightly More Advanced ChatBot [Intermediate, Python]
Profile icon
ChristianThone

Hello Fellow Replers!

Welcome to my next tutorial with a ChatBot, if you haven't seen my last tutorial, then you should click here.

How do we make our ChatBot more advanced?

That is easy to answer first we need to think, how would this be possible? With connecting our ChatBot to google and find the most relevant answer to the User Input? Good idea, but no. In fact we can make even simpler, and I'm going to explain. We can use two lists! Now this is really cool, and simple. A lot of people don't know how to do this. You will after reading the rest of this tutorial.

How can we use a list to detect User Input?

Well, we can't do that, but we can use it so the ChatBot can figure out how to respond to the User. Let me give you a small explanation about how this is possible.

First, we have two lists:

Check
Respond

If we assign values then they will look like this:

Check = ["hello", "how are you?"] Respond = ["Hi.", "I'm good.']

Do you notice anything?

The User Inputs and the Responses have the same List Index Value(LIV). Let me show you.

If you look at list Check you will see the first item: hello this has a LIV of 0.

And if you look at list Respond you will see the first item: Hi this also has a LIV of 0.
Do you see a correlation between the two? Maybe you do, maybe you don't you will understand later in the next section.

Now to the code!

Alright so we have the basic code we wrote in the last tutorial. That code was simple, and impractical. Let's simplify it.
Here is the code:

UserInput = "" ChatBot_Name = "Ozzy" UserInput = str.lower(input(": ")) if UserInput == "hi": print(ChatBot_Name + ": Hello!") elif UserInput == "who made you?": print(ChatBot_Name + ": I was made by Christian Thone")

Lets first create a new file, you can do that by clicking the little paper icon on the side of you project:

This file cannot be displayed: https://storage.googleapis.com/replit/images/1551187092327_54d076adc829d1b11f9b6abac92c3275.pn

Now go to the smaller paper Icon with the plus sign and click that. Now you have created a file, for the name of your file name it client.py. There is no specific reason for the name it just looks cool. Ok, back to the tutorial.

Inside of client.py create the following lists:

User_Inputs = [] __Responses = []

Now obviously we have nothing in the lists, but that is what we want right now.
Hmmm? So how do we use these lists? Easy! We use what Repl.it has called import. Type the following into main.py at the top of the script:

from client import User_Inputs, __Responses

Now we can use these lists.

We need some values in these lists for anything to happen, so write the following into your lists in client.py:

# REMEMBER TO HAVE YOUR USER_INPUTS IN LOWERCASE! User_Inputs = ["hi"] __Responses = ["Hello."]

Okay cool, now we have some simple code for our lists, but we still cant do anything with our ChatBot yet. First, we need to tell it what to do. Go into main.py and type the following:

if UserInput in User_Inputs: index = User_Inputs.index(UserInput) print(ChatBot_Name + __Responses[index])

Is the LIV thing making sense to you? If not I will explain it now, skip this if you understand it

So basically I am using the LIV, to tell the ChatBot how to respond to user input.

Look at this and you might understand. In the lists we have two different, items. hi, hello
The easiest way to look at this is by understanding that they are in the same position just a different list.

Here is a visual

['hi']
| 0
['Hello.']
They match up with the same index that is why I can print the index of User_Inputs, through __Responses.

Now here is the finished output:

This file cannot be displayed: https://storage.googleapis.com/replit/images/1551188357482_f1285e9a35d77c937492742df8e7b616.pn

If you have any questions then post them in the comments.

Stay tuned for
"An Advanced ChatBot [Advanced, Python]", I will show you how to have your bot scan User Input for Keywords!.

Keep coding,
Christian Thone
@ChristianThone

Voters
Profile icon
mbrahmbhatt512
Profile icon
DanielleSoh
Profile icon
YashGaneshgudi1
Profile icon
Dat1Agent
Profile icon
aguy11
Profile icon
awodeay1
Profile icon
ash15khng
Profile icon
Makrayne
Profile icon
MikeStriker
Profile icon
millanc
Comments
hotnewtop
Profile icon
slimaidarismail

this is too basic, looking forward an 'advanced bot' Thaanks

Profile icon
ChristianThone

@slimaidarismail You're welcome, the advanced bot is taking a while to make, sorry for the wait.

Profile icon
YashGaneshgudi1

@ChristianThone Are you still planning on posting the advanced bot?

Profile icon
DanielleSoh

@ChristianThone is it out yet?

Profile icon
ash15khng

Suggestion: use a dictionary instead of 2 lists. It's really easy to mess up and get your bot to output the wrong thing.

Profile icon
ChristianThone

@ash15khng well thats why you should take your time and mke sure it is done right, and you should always test your programs after adding something to it. What do you mean by use a dictionary? Can you give me an example?

Profile icon
ash15khng

@ChristianThone This is an example of a dictionary and an updated code using it:

responses = {"hi": "hello", "how are you": "i'm good"} userinput = input() if userinput in responses.keys(): print(responses[userinput]) else: print("? i don't understand")
Profile icon
joshwood

@ChristianThone Yeah, a dictionary would probably make more sense for this project. It would improve readability.

Profile icon
ChristianThone

@ash15khng Okay, I will add it to the Advanced bot, but if anyone knows how to scan for keywords that would be great if you could tell me.

Profile icon
joshwood

@ChristianThone You can use string in other_string.
For example:

if "apple" in "banana": print("yay") elif "apple" in "banana and apple": print("yayayayay")

This would print "yayayayay".

Profile icon
Murzikal

@ChristianThone you can also split the input using the split method
Example:

def keyCheck(keyword, userInput, answer): iList = userInput.split(" ") # input split if keyword in iList: print(answer) else: print("Sorry, I don't understand.") return; userInput = input("Say something. ") userInput = userInput.lower keyCheck("hello", userInput, "hi")

this would return hi if the word hello was in the input. Also, this may not be 100% correct, so double check before using.

Profile icon
Murzikal

@ChristianThone

a better example, where you check for everything:

def keyCheck(keyword, userInput, answer): iList = userInput.split(" ") # input split if keyword in iList: print(answer) else: print("Sorry, I don't understand.") return; while True: userInput = input("Say something. ") userInput = userInput.lower() for i in inputs: # inputs is the list of inputs you imported earlier keyword = inputs[i] answer = responses[i] keyCheck(keyword, userInput, answer)

this is my attempt, and maybe you could replicate this, but with dictionaries as well, to make the code better ;)

Profile icon
yoonji1010

@ChristianThone i really dont properly understand this can you help me?

Profile icon
YashGaneshgudi1

@ChristianThone Are you still planning on making an advanced bot tutorial?

Profile icon
Murzikal

cool

Profile icon
ChristianThone

@Murzikal Did you try it?

Profile icon
Murzikal
Profile icon
Murzikal

@ChristianThone i knew most of it though, except import from a file.

Profile icon
ChristianThone

@Murzikal Ok, at least you learned something huh? Thats what i'm here for.

Profile icon
Murzikal
Profile icon
Superhero25e

How can I actually add it to a chatting service bro?

Profile icon
MikeStriker

thanks dude I had a lot of trouble trying to figure out how to get the file to work and some other things from this I was able to use and I have checked out a LOT!! of your other things and they have helped me figure out how to do things before I have even gotten to that stage! Your stuff is AMAZING!

Profile icon
ZControls_Games

Noice

Profile icon
ChristianThone

@ZControls_Games Thank you!