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 spe…


This content originally appeared on DEV Community and was authored by Rishabh Singh ⚡

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


This content originally appeared on DEV Community and was authored by Rishabh Singh ⚡


Print Share Comment Cite Upload Translate Updates
APA

Rishabh Singh ⚡ | Sciencx (2021-02-19T10:40:40+00:00) How to build an Echo Chatbot in Python. Retrieved from https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/

MLA
" » How to build an Echo Chatbot in Python." Rishabh Singh ⚡ | Sciencx - Friday February 19, 2021, https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/
HARVARD
Rishabh Singh ⚡ | Sciencx Friday February 19, 2021 » How to build an Echo Chatbot in Python., viewed ,<https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/>
VANCOUVER
Rishabh Singh ⚡ | Sciencx - » How to build an Echo Chatbot in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/
CHICAGO
" » How to build an Echo Chatbot in Python." Rishabh Singh ⚡ | Sciencx - Accessed . https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/
IEEE
" » How to build an Echo Chatbot in Python." Rishabh Singh ⚡ | Sciencx [Online]. Available: https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/. [Accessed: ]
rf:citation
» How to build an Echo Chatbot in Python | Rishabh Singh ⚡ | Sciencx | https://www.scien.cx/2021/02/19/how-to-build-an-echo-chatbot-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.