How to build an Echo Chatbot in Python

mindninjax

Rishabh Singh ⚡

Posted on February 19, 2021

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?

Alt Text

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...")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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: ")
Enter fullscreen mode Exit fullscreen mode

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}")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Here we did it! Awesome 🤩

Source Code

You can find the complete source code of this project here -

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

💖 💪 🙅 🚩
mindninjax
Rishabh Singh ⚡

Posted on February 19, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related