Skip to main content
Skip to main content

Pencil code program: Lady Macbeth chat bot

Integrating Digital Technologies
Years 7-8; 9-10

DT+ English

Use this program to create an interactive chat bot who answers questions as if she is Lady Macbeth. Have students analyze, fill in or change parts of, or use the program to create their own variation and rendition of a character. This program could be used to further your understanding of how you could use Pencil Code in the classroom, as a demonstration or discussion with your students, or as a way to introduce various CT concepts, such as pattern recognition or abstraction, to your students by inviting them to extend the existing functionality of the program.

Based on the original lessons developed by the Exploring Computational Thinking team at Google.

Learning hook

Provide the quote:

‘Thy bones are marrowless, thy blood is cold
Thou hast no speculation in those eyes’

Have students guess which literature character said those words. (Lady Macbeth)

Lady Macbeth is a character from Shakespeare that students may be familiar with through studies of English literature.

In programming we can use this context to create a chatbot that responds to a user’s text input in a style that would reflect the Lady Macbeth character. To acheive this, we will be using the pencil code program

Ask students to recall particular scenes or quotes from the famous Shakespeare play. To refresh students’ memory use this study guide Lady Macbeth.

Learning input

Provide students with the chatbot pencil code program. Copy/Paste the program into a ‘Blank Editor’ on the Pencil Code website (

Explore how the chatbot responds to particular text inputs.

Can you work out what words the chatbot will respond to?

Learning construction

Have students analyze or fill in or change parts of the pencil code program.

Ways to remix the program

  1. Change or add to the chatbot responses for each user input.
  2. Add new keywords and chatbot responses.
  3. Change the introductory text to invite the user to enter text.
  4. Create their own code in python to respond as Lady Macbeth.

Learning reflection: Refining an algorithm based on new constraints (10 minutes)

How well did the pencil program work?

What changes or modifications did you make?

How well did these changes work?

Computational Thinking Concepts*

* Explore the Computational Thinking Concepts Guide for a list of the CT concepts noted on ECT, including tips for implementing each concept in your classroom.

Pencil code program

Copy/Paste the following program into a ‘Blank Editor’ on the Pencil Code website (

# Copyright 2015 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

replyto = (words) ->
  for word in words
    switch word
      when "hello"
        return "Hello. I'm waiting."

      # For example, if asked "Who are you?"
      when "name", "who"  
        return "Lady Macbeth."

      # E.g.: "Have you heard about the murder?"
      when "power", "kill", "duncan", "murder", "king" 
        return """
          I love power.
          I will do anything for power.
      when "prophecy" , "prophesy" , "prophesies"
        return """
          I think the prophesies are true,
          but I think that we have to make them
            come true. We can't just rely on fate.
          We have to take matters into our own hands.
      when "witches", "fair", "foul"
       return """
          They say that Fair is foul,
          and foul is fair, and I agree.

      when "blood", "hand"
        return """
          All we have to do is wash our hands of it.

      when "plan", "idea", "plans", "plot"
        return """
          All we have to do is wash our hands of it."
  return random [
    "Are you a man?"
    "Was hope drunk wherein you dressed yourself?"
    "Only look up clear. To alter favor ever is to fear. Leave all the rest to me."

while true
  await readstr "Ask me anything.", defer t
  words = t.toLowerCase().split /[\s\.,!?]+/
  write replyto words

Sample output

Sample Pencil Code output that states: Ask me anything. What art thou? Only look up clear. To alter favor ever is to fear. Leave all the rest to me. Ask me anything.

Additional information and resources