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 🤩
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.
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
Top comments (0)