Build a chatbot in Scratch, then break it on purpose

A small toy robot standing beside an open laptop

Ask a child how ChatGPT knows what to say and you will usually get a shrug, or “it looks it up”. Neither is right, and explaining it in words rarely lands. Build the other kind of chatbot together first, the kind that only knows what you told it, and then spend ten minutes trying to break it. You end up with a talking character on screen and a much better feel for how ChatGPT works.

The free project comes from the Raspberry Pi Foundation, the UK educational charity, and is called simply Chatbot. It is one of the foundation’s first-level Scratch projects and runs in a browser.

What you will end up with

A cartoon robot on a space backdrop. Click it and it asks your name, greets you by it, asks whether you are OK, pulls a happy or sad face depending on your answer, then offers to take you to the moon and bounces up and down when you say yes.

Building it: the steps

Let your child hold the mouse. The foundation’s guide shows the exact blocks; this is the route.

  1. Give it a personality first. The guide asks four questions before any code: what is the chatbot called, where does it live, is it happy, serious, funny or shy, and what does it like and dislike? Write the answers down.
  2. Open the starter project at rpf.io/chatbot-on. If you have a Scratch account, click Remix to keep your own copy. Pick a character and click the eye icon to show it on the stage.
  3. Make it talk. On the chatbot sprite, snap together when this sprite clicked, then ask [What's your name?] and wait, then say [What a lovely name!] for (2) seconds. Click the robot and type a name.
  4. Make it use the name. Swap the reply for say (join [Hi ] (answer)). Then, under Variables, click Make a Variable, call it name, and add set [name v] to (answer) and greet with name instead.
  5. Teach it to decide. Add ask (join [Are you OK ] (name)) and wait, then an if <(answer) = [yes]> then ... else block. Put say [That's great to hear!] in the top half and say [Oh no!] in the bottom. Test it twice, as the guide says: once with “yes”, once with “no”.
  6. Give it faces. Inside each half of the if block, add switch costume to so the robot looks happy or sad. (The guide’s costume names belong to its Nano character; for another character, use the dropdown.) Then fix the bug the guide points out: add a switch costume to at the very top so the face resets each time.
  7. Go to the moon. Add ask [Do you want to go to the moon?] and wait with another if <(answer) = [yes]> that switches the backdrop to the moon. Put switch backdrop to (space v) at the top so it always starts at home. A repeat (4) loop makes it bounce.

[[photo:1]]

Now break it

The guide only hints at this, suggesting friends try the chatbot to see whether “they can spot any problems”. Make it a game. Click the robot, and when it asks “Are you OK?”, try these one at a time:

  • yes (works)
  • YES (also works; the guide notes the check is not case-sensitive)
  • yeah
  • yes!
  • I'm great, thanks

The last three all get “Oh no!” and a sad face. The robot is doing exactly what it was told: if the answer is the word “yes”, be happy, and if it is anything else at all, be sad. “I’m great” is not the word “yes”.

Let your child fix it. Scratch has an operator block called contains, which checks whether one piece of text appears inside another and, like the equals check, ignores capital letters. Swap (answer) = [yes] for (answer) contains [yes]? and test again. “yes!” now works. Then type yesterday was awful. The robot beams and says “That’s great to hear!”, because “yesterday” contains “yes”.

Every fix opens a new hole. Keep a list of what fooled it.

[[photo:2]]

What the broken robot just taught you

Your chatbot is a rule-based program. Every reply exists because somebody put a block there. It will never say anything you did not plan for, never make up a fact, and never understand “yeah nah”.

ChatGPT is the other approach. Nobody wrote an if block for “I’m great, thanks”. Google’s own machine learning course describes large language models as predicting “a token or sequence of tokens”, and puts it bluntly: they “are essentially autocomplete mechanisms”. They learned the patterns of language from enormous amounts of text, which is why they cope with any phrasing you throw at them. It is also why they can produce a fluent, confident answer that is simply wrong, something your Scratch robot is incapable of. (We have written separately about what a token is and why chatbots make things up.)

This difference is not a side issue in British classrooms. Experience AI, the course the Raspberry Pi Foundation built with Google DeepMind for 11 to 14 year olds, opens its very first lesson on “the differences between rule-based and data-driven approaches to programming”. Your version gets there with a sulking robot.

It is also a decent answer to the question of whether coding still matters now that AI can write code. Philip Colligan of the Raspberry Pi Foundation has argued that it does, and one of his reasons is that “coding is a literacy that helps young people have agency in a digital world”. A child who has written the rules can picture what a machine without rules is doing instead.

The Singapore read

Many children here will recognise the blocks. Singapore’s Code for Fun programme, or a comparable coding programme, is mandatory for all upper primary students, and since 2025 schools have been able to add elective AI for Fun modules of five to 10 hours, according to a 2024 announcement from the Ministry of Digital Development and Information. This project bridges the two: blocks they may know, pointed at the AI question.

One honest note: Mentus AI makes AI mentors for children, which are data-driven chatbots of exactly the kind described above. We think a child who has built the rule-based kind uses ours, and anyone else’s, with a healthier raised eyebrow.

Tonight’s prompt, once the robot is built: ask your child to write one answer it will get wrong before they type it. If they can predict the failure, they understand the machine.

Was this useful?

Sources

  1. Chatbot (Scratch project, version 4.2.0) · Raspberry Pi Foundation
  2. Experience AI: The excitement of AI in your classroom · Raspberry Pi Foundation
  3. Learning to code in an AI age · Raspberry Pi Official Magazine
  4. LLMs: What's a large language model? · Google for Developers (Machine Learning Crash Course)
  5. () Contains ()? (Operators block) · Scratch Wiki
  6. New "AI for Fun" modules for students · Ministry of Digital Development and Information, Singapore