Course F - Variables 2 - Variables and Emotions

Students create programs with events, variables, and conditionals.

  • Computer-science
  • Variables
  • Functions
  • Conditionals
Print lesson
  • Ages 10+
  • 90 mins
  • Javascript Blocks

はじめに

This is a continuation of the lesson 'VARIABLES WITH A COUNTER'. While 'VARIABLES WITH A COUNTER' focused on variables that stored numbers, here we will store text or strings in our variables. Strings are alphanumeric characters but we do not use them to do math. Instead we mostly use these to display words. Also, storing words in our variables helps to make things more readable sometimes.

Variables change a lot like our emotions do. This lesson asks us to change string variables. We will do this to try and make our baby brother happy. Read on to learn more about the connected to theme to this lesson but remember the focus is string variables and how to manipulate them in the micro:bit.

Our micro:bit course lessons are tailored to apply knowledge obtained from the Code.org CS Fundamentals. Before students begin these Course Lessons, we encourage students to first complete all CS Fundamentals as a prerequisite. Students should be familiar with variables from Code.org CS Fundamentals.

Students and teachers are highly encouraged to first complete the prerequisites in the resources section above.

Teacher Guide

Open

Activity

What you'll need

  • 1 - micro:bit
  • 1 - USB cable
  • 1 - AAA battery pack (optional)
  • 2 - AAA batteries (optional)

Documentation

basic.forever(() => {})

forever

Keep running part of a program in the background.


for(let i = 0; i < 5; ++i) {
  basic.showNumber(i)
}
            

for

Run part of the program the number of times you say.


basic.showString("Hello!")
            

showString

Show a string on the LED screen


basic.showNumber(0)
            

showNumber

Show a number on the LED screen


let item = 0
item = 0
            

setItem

Use an equals sign to make a variable store the number or string you say.


function doSomething()  {
    basic.showString("Hello!")
}

doSomething()
            

function

A function lets you create a portion of code that you can reuse in your program.

4 steps

Guided Practice

We will make instructions or code online for the micro:bit. Next, go to: makecode.microbit.org

let Result = ""
let What_to_give = ""
let Morning_Mood = ""
function How_Does_Baby_Brother_Feel_Now()  {
    basic.showString(Morning_Mood)
    for (let i = 0; i < 3; i++) {
        basic.showLeds(`
            . . # . .
            . . # . .
            # # # # #
            . . # . .
            . . # . .
            `)
        basic.clearScreen()
        basic.pause(250)
    }
    basic.showString(What_to_give)
    for (let i = 0; i < 5; i++) {
        basic.showLeds(`
            . . . . .
            . # # # .
            . . . . .
            . # # # .
            . . . . .
            `)
        basic.clearScreen()
        basic.pause(100)
    }
    if (Result == "Happy") {
        basic.showIcon(IconNames.Happy)
    } else if (Result == "Sad") {
        basic.showIcon(IconNames.Sad)
    } else if (Result == "Confused") {
        basic.showIcon(IconNames.Confused)
    } else if (Result == "Angry") {
        basic.showIcon(IconNames.Angry)
    } else {
        basic.showIcon(IconNames.Meh)
    }
}
basic.forever(() => {
    Morning_Mood = "Sad"
    What_to_give = "Toy"
    Result = "Happy"
    How_Does_Baby_Brother_Feel_Now()
})

Imagine you have a baby brother. Every morning, his mood is different. You are babysitting him and you try to deal with his emotions. You are not always successful at calming him but you try. For each mood he presents, you need to give him something. Then you hope it makes him happy but it doesn't always work.

In our program we will show this with string variables. An example of output might be:

Sad + Toy = Happy

Here our brother woke up sad. Then we gave him a toy. Later, he was happy.

We start our program in the forever loop. A variable called 'Morning_Mood' is set to Sad. For this first example, baby brother wakes up sad.

Next, a variable called 'What_to_give' is set to Toy. You try to give your brother a toy because he is sad. Finally, a variable called 'Result' is set to Happy. Giving your brother a toy makes him happy!

The call to the function 'How_Does_Baby_Brother_Feel_Now' displays our string variables. The variables will scroll across the screen.

Follow the link to the starter code. There are extra blocks at the bottom to help you get started

The starter code is available here.

NOTE: The unused blocks at the bottom of the code are meant to be used in the challenges. Also, there is a big function called 'How_Does_Baby_Brother_Feel_Now'. It is not necessary to cover or understand this function. It is only there to help show our output in a more meaningful way. Should you wish to explore it, do so. Otherwise you can ignore it.

Variables and Emotions Challenges

The starter code is available here.

Challenge 1

Change the values of all three variables (Morning_Mood, What_to_give, Result) to create a new situation for baby brother. Your story should make sense.

Note: Any Result other than (Happy, Sad, Confused, or Angry) will display a meh face!

View code
let Result = ""
let What_to_give = ""
let Morning_Mood = ""
function How_Does_Baby_Brother_Feel_Now()  {
    basic.showString(Morning_Mood)
    for (let i = 0; i < 3; i++) {
        basic.showLeds(`
            . . # . .
            . . # . .
            # # # # #
            . . # . .
            . . # . .
            `)
        basic.clearScreen()
        basic.pause(250)
    }
    basic.showString(What_to_give)
    for (let i = 0; i < 5; i++) {
        basic.showLeds(`
            . . . . .
            . # # # .
            . . . . .
            . # # # .
            . . . . .
            `)
        basic.clearScreen()
        basic.pause(100)
    }
    if (Result == "Happy") {
        basic.showIcon(IconNames.Happy)
    } else if (Result == "Sad") {
        basic.showIcon(IconNames.Sad)
    } else if (Result == "Confused") {
        basic.showIcon(IconNames.Confused)
    } else if (Result == "Angry") {
        basic.showIcon(IconNames.Angry)
    } else {
        basic.showIcon(IconNames.Meh)
    }
}
basic.forever(() => {
    Morning_Mood = "Hungry"
    What_to_give = "Bottle"
    Result = "Full"
    How_Does_Baby_Brother_Feel_Now()
})

hungry + bottle = meh face... I guess your brother is not hungry yet.

Challenge 2

This time it is Night time. Instead of giving your brother something, he wants to do an activity. Rename the two variables(Morning_Mood, What_to_give). You should rename them to something meaningful. Our situation has changed and so the variable need new names to show this. Then change the values of the three variables again to create a new situation at night for baby brother. Your story should make sense.

View code
let Result = ""
          let Activity_to_do = ""
          let Night_Mood = ""
          function How_Does_Baby_Brother_Feel_Now()  {
              basic.showString(Night_Mood)
              for (let i = 0; i < 3; i++) {
                  basic.showLeds(`
                      . . # . .
                      . . # . .
                      # # # # #
                      . . # . .
                      . . # . .
                      `)
                  basic.clearScreen()
                  basic.pause(250)
              }
              basic.showString(Activity_to_do)
              for (let i = 0; i < 5; i++) {
                  basic.showLeds(`
                      . . . . .
                      . # # # .
                      . . . . .
                      . # # # .
                      . . . . .
                      `)
                  basic.clearScreen()
                  basic.pause(100)
              }
              if (Result == "Happy") {
                  basic.showIcon(IconNames.Happy)
              } else if (Result == "Sad") {
                  basic.showIcon(IconNames.Sad)
              } else if (Result == "Confused") {
                  basic.showIcon(IconNames.Confused)
              } else if (Result == "Angry") {
                  basic.showIcon(IconNames.Angry)
              } else {
                  basic.showIcon(IconNames.Meh)
              }
          }
          basic.forever(() => {
              Night_Mood = "Bored"
              Activity_to_do = "Play Ball"
              Result = "Sad"
              How_Does_Baby_Brother_Feel_Now()
          })
          

It appears from this example that the boy does not like to play with a ball. To get full credit, I would probably need to explain why because it seems strange that a young child does not like to play ball.

Challenge 3

Delete 'call function How_Does_Baby_Brother_Feel_Now' in the forever loop. Use the code block with show string join to show your output instead. Join can put our string variables together for us instead of using that big function.

Hint: Use the extra blocks that were added to the code at the bottom.

View code
let Result = ""
let Activity_to_do = ""
let Night_Mood = ""

basic.forever(() => {
    Night_Mood = "Bored"
    Activity_to_do = "Play Ball"
    Result = "Sad"
    basic.showString("" + Night_Mood + Activity_to_do + Result)
})

Note: The students variables will be different at this point so the code will be a bit different probably. The important thing is the outcome.

Independent Practice

Put two words together to make a bigger word. This is called a compound word. Use the 'How_Does_Baby_Brother_Feel_Now' function, join, or some other methods to combine two text variables. Try to rename the variables that will store your two words to something meaningful.

Example - Some examples of words you might make would be (play + ground = playground) or (tree + house = treehouse). The variable names themselves might be word1 and word2.

Note: The output does not need to be a real compound word. Make up your own word if you can like 'headfork'. Then have fun telling everyone about the meaning of your new word or why it was made.

Quiz

Use these questions to reflect on the activity and challenges.

Question 1

What do we use variables for? What is the purpose of variables?

Show answer

Answers may vary but they are places where you can store and retrieve data.

Question 2

What is one way to put text variables together?

Show answer

The join method can put two or more text(string) variables together.

Question 3

What will the output of the code below be?

set variable to "yes"
set variable to "no"
set variable to "maybe"
set variable to "oh... ok!"

show string variable
Show answer
oh... ok!