Real AI engineers write code. Don't worry — we'll start gently, and you'll have a working program by the end of this chapter. Code is just clear instructions for a computer, written one line at a time.
We'll use Python, the language most AI is built with. It reads almost like English.
Step 0 — Open a Python notebook
You'll write code in a free browser tool, so there's nothing to install.
- With a grown-up, open Google Colab (colab.research.google.com) and start a New notebook. (A "notebook" lets you run little chunks of code one at a time — perfect for learning.)
- You'll type code into a cell and press the ▶ play button (or Shift+Enter) to run it.
Stay safe
Colab needs a Google account, so set it up with a parent or guardian (or use their account with them nearby). Notebooks you make are private to you. As always, don't put real personal details in your code.
Step 1 — Make the computer talk
Type this and run it:
print("Hello! I am your first program.")
print(...) shows whatever is inside the quotes. You just gave the computer an
instruction and it obeyed. 🎉
Step 2 — Variables: boxes that remember
A variable is a labeled box that stores a value:
name = "Robo"
age = 1
print(name + " is " + str(age) + " year old.")
nameholds text (called a string).ageholds a number.str(age)turns the number into text so it can join the sentence.
Step 3 — Ask the user a question
input(...) lets your program ask and remember the answer:
user_name = input("What's your name? ")
print("Nice to meet you, " + user_name + "!")
Run it, type your name, and watch your program reply. Now it's interactive.
Step 4 — Make decisions with if / else
Programs get smart when they choose:
mood = input("How are you? (good/bad) ")
if mood == "good":
print("Awesome! Let's build something. 🚀")
else:
print("That's okay. Coding always cheers me up!")
Notice the indentation (the spaces). Python uses it to know which lines
belong to the if. Keep it tidy — it matters!
Step 5 — Repeat with a loop
A loop repeats lines without copy-pasting:
for i in range(3):
print("Counting... " + str(i + 1))
This runs three times. Loops will let your chatbot keep talking instead of stopping after one reply.
Check yourself
- What does
print()do? - What's a variable?
- What does
input()give you, and why isif / elseuseful?
Project — Build a rule-based chatbot 🛠️
This chatbot uses rules you write (no AI yet — that's Chapter 7!). It shows you exactly how a program "thinks."
print("🤖 QuizBot: Hi! Answer my questions. Type 'quit' to stop.")
score = 0
questions = [
("What planet do we live on? ", "earth"),
("What color is the sky on a clear day? ", "blue"),
("2 + 2 = ? ", "4"),
]
for question, answer in questions:
reply = input(question).lower().strip()
if reply == "quit":
break
if reply == answer:
print("✅ Correct!")
score = score + 1
else:
print("❌ Not quite. The answer was: " + answer)
print("🤖 You scored " + str(score) + " out of " + str(len(questions)) + "!")
- Type it in (typing it yourself helps you learn more than pasting).
- Run it and play your quiz.
- Make it yours: add your own questions to the list, change QuizBot's name, or add a victory message when the score is perfect.
Your turn
Add a personality. Give your bot a name variable and have it use the
player's name in every reply. Then add a final if score == len(questions):
message like "🏆 Perfect score, genius!"
Make it simpler · ages 9–11
Just do Steps 1–4 with a grown-up. The big win is making the computer ask
your name and reply with an if / else. That tiny program is real coding — be
proud!
Level up · ages 13–16
Refactor your quiz into a function so you can reuse it:
def ask(question, answer):
return input(question).lower().strip() == answer
Then call ask(...) for each question and count the True results. Functions
are how engineers avoid repeating themselves — you'll lean on them hard in the
next chapters.
What you learned
print, variables, andinputlet your program talk and listen.if / elsemakes decisions; loops repeat work.- You built a real rule-based chatbot in Python.
You've earned the Code Cadet badge. 🏅
Your bot follows rules you wrote. Next, you'll plug a real AI brain into your code, so it can answer anything. That's Chapter 7: Your First AI App in Code.