How to build an Echo Chatbot in Python

How to build an Echo Chatbot in Python:

 Hello everyone! Long time, no see… Well, today we are going to create a simple Echo Chatbot in Python.

How does it work?

An Echo Chatbot is basically a program which can repeat the phrases of the user.

And it keeps running until a specific phrase is received from the user.

Super quick and fun project! Let’s get into coding.

Let’s Code

First, let’s print a greeting message and also instruct the user on how to end the conversation.

print("Let's talk! Enter 'quit' to exit...")

The conversation will continue until the user has a specific input which in our case is quit.

Let’s create a while loop to keep the program executing.

while True:
    pass

Alright, so the first thing we are going to do is to ask the user to input some text and start the conversation.

while True:
    user = input("You: ")

Now it’s our bot’s turn to reply to the user. Since it’s a simple echo chatbot, it will repeat what the user has said.

while True:
    user = input("You: ")
    print(f"Bot: {user}")

This can simply be achieved through using an print() statement and printing variable within it. We are using f-string method which is a new way of printing variables within print() statement in Python 3.

Now for one last step, if the user passed the input of quit keyword then out program should stop running.

Let’s use an if conditional to break the loop if quit is found in the user input.

while True:
    user = input("You: ")
    print(f"Bot: {user}")
    if user == 'quit':
          break

Here we did it! Awesome 

from Tumblr https://generouspiratequeen.tumblr.com/post/643625766509232128

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s