Build Pybot, a friendly chatbot that remembers your name, answers questions and tells jokes.
Chatbots are programs that you can talk to by typing. Big chatbots use huge amounts of data, but you can build a fun one with just a few lines of Python. In this project you will create Pybot, a cheerful little robot that asks your name, chats with you, and tells terrible computer jokes.
Pybot works by looking for keywords. If your message contains the word "joke", Pybot tells a joke. If it contains "how are you", Pybot tells you how it feels. This is how many early chatbots worked, and it is a brilliant way to practise working with strings and making decisions.
You will put all of Pybot's thinking inside a function called get_reply(). That keeps your code tidy: the main loop only has to read the message and print the reply, while the function does the hard work. Once it is working, you can give Pybot its own personality!
Pybot starts by introducing itself and asking for your name. We store the answer in a variable called name so Pybot can use it later. Joining strings with + lets us build a personal greeting.
print("đ¤ Hi! I'm Pybot, a tiny chatbot.")
name = input("What's your name? ")
print("Nice to meet you, " + name + "!")
đ¤ Hi! I'm Pybot, a tiny chatbot. What's your name? Leo Nice to meet you, Leo!
get_reply() takes the player's message and returns Pybot's answer. The in keyword checks whether one string appears inside another, so "hello" in message is True for "hello there" too. If no rule matches, the else gives a default reply.
def get_reply(message):
if "hello" in message:
return "Hello to you too! đ"
else:
return "Hmm, I don't understand that yet."
print(get_reply("hello there"))
print(get_reply("what is a banana?"))
Hello to you too! đ Hmm, I don't understand that yet.
Each elif is a new thing Pybot understands. We use message.lower() first so that "HELLO" and "Hello" both match "hello". We also pass in the player's name so Pybot can use it in its answers.
def get_reply(message, name):
message = message.lower()
if "hello" in message or message == "hi":
return "Hello again, " + name + "! đ"
elif "how are you" in message:
return "I'm feeling electric! ⥠Thanks for asking."
elif "your name" in message:
return "My name is Pybot. I live inside your computer!"
else:
return "Hmm, I don't understand that yet. Try asking for a joke!"
print(get_reply("How are you?", "Leo"))
print(get_reply("HELLO", "Leo"))
I'm feeling electric! ⥠Thanks for asking. Hello again, Leo! đ
We store some jokes in a list. random.choice() picks one item from a list at random, so Pybot tells a different joke each time. Add this list near the top of your program, then add the joke rule to get_reply().
import random
jokes = [
"Why do programmers prefer dark mode? Because light attracts bugs! đ",
"Why was the computer cold? It left its Windows open! đĨļ",
"What do you call a snake that does maths? A Py-thon! đ"
]
print(random.choice(jokes))
Why do programmers prefer dark mode? Because light attracts bugs! đ
đ˛ Your joke might be different, because random.choice() picks one at random!
A conversation goes back and forth, so we use while True: to keep chatting. Each time round, we read a message. If it is "bye", Pybot says goodbye and break ends the loop; otherwise we print Pybot's reply and the loop starts again.
name = "Leo"
while True:
message = input(name + ": ")
if message.lower() == "bye":
print("Pybot: Goodbye, " + name + "! Come back soon! đ")
break
print("Pybot: You said", message)
Leo: hello Pybot: You said hello Leo: Bye Pybot: Goodbye, Leo! Come back soon! đ
Here is the whole program with every step put together. If something isn't working in your version, compare it with this one line by line. Check the spelling, the colons at the end of if and while lines, and that the indentation (the spaces at the start of lines) matches.
import random
jokes = [
"Why do programmers prefer dark mode? Because light attracts bugs! đ",
"Why was the computer cold? It left its Windows open! đĨļ",
"What do you call a snake that does maths? A Py-thon! đ"
]
def get_reply(message, name):
message = message.lower()
if "hello" in message or message == "hi":
return "Hello again, " + name + "! đ"
elif "how are you" in message:
return "I'm feeling electric! ⥠Thanks for asking."
elif "your name" in message:
return "My name is Pybot. I live inside your computer!"
elif "joke" in message:
return random.choice(jokes)
elif "colour" in message or "color" in message:
return "My favourite colour is Python blue! đ"
else:
return "Hmm, I don't understand that yet. Try asking for a joke!"
print("đ¤ Hi! I'm Pybot, a tiny chatbot.")
name = input("What's your name? ")
print("Nice to meet you, " + name + "!")
print("Type bye when you want to stop chatting.")
while True:
message = input(name + ": ")
if message.lower() == "bye":
print("Pybot: Goodbye, " + name + "! Come back soon! đ")
break
print("Pybot:", get_reply(message, name))
This is what it looks like when someone plays. The words after each question are what the player typed.
đ¤ Hi! I'm Pybot, a tiny chatbot. What's your name? Leo Nice to meet you, Leo! Type bye when you want to stop chatting. Leo: hi Pybot: Hello again, Leo! đ Leo: What is your name? Pybot: My name is Pybot. I live inside your computer! Leo: tell me a joke Pybot: Why do programmers prefer dark mode? Because light attracts bugs! đ Leo: What's your favourite colour? Pybot: My favourite colour is Python blue! đ Leo: Can you fly? Pybot: Hmm, I don't understand that yet. Try asking for a joke! Leo: bye Pybot: Goodbye, Leo! Come back soon! đ
Finished? Brilliant! Real programmers are always improving their programs. Try one of these challenges to make the project truly yours.
Stuck on a step? These lessons explain the Python ideas used in this project.