Course F - Variables 1 - Variables With A Counter

Students create programs with events, variables, and conditionals.

  • Computer science
  • variables
  • events
  • conditionals
Print lesson
  • 年龄 10+
  • 90 分钟
  • JavaScript Blocks

介绍

This lesson introduces variables. A variable is a place to save information(numbers, text, etc) so we can grab it later. We use this saved information to show to people or make decisions. We will use variables in this lesson to create a counter.

The variables we use in this lesson will be numbers but in the second lesson of this series, 'VARIABLES AND EMOTIONS', we focus on text or string variables. We will use mathematical operators to change our variables.

One important thing to remember when using variables is to name your variables in a meaningful way. For example, if you want to save the number of basketballs your school has, you might name your variable 'basketballs' or 'number_of_basketballs'. However, a plain variable like 'x' is not recommended. Following this tip will make understanding your code and finding problems much easier.

How to make variables and name them is not explicitly covered in this lesson. It would be wise to demonstrate this before having students try this lesson on their own.

我们的micro:bit课程是根据Code.org CS 基础课程知识应用而量身定做的。 在学生们开始这些课程之前,我们鼓励学生们首先完成所有的CS基础知识。 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.

教学指南

打开

课堂活动

你将需要这些:

  • 1块micro:bit
  • 1根USB线
  • 1个AAA电池组(可选)
  • 2节AAA电池(可选)

编写文本


basic.showNumber(1 - 1)
basic.showNumber(1 + 1)
basic.showNumber(1 * 1)
basic.showNumber(1 / 1)
            

Mathematical operators

Evaluate mathematical expressions


input.onButtonPressed(Button.A, () => {

})

            

当按钮被按下

Start an event handler (part of the program that will run when something happens, like when a button is pressed)


basic.showNumber(0)
            

showNumber

在LED屏幕上显示数字。


让 项目= 0
项目 = 0
            

设置项目

用一个等号让一个变量存储数字或你指定的字符串。

4个步骤

指导练习

我们将在网上给出micro:bit的教程或代码。接下来,去到:makecode.microbit.org


let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
        

We start our program with a variable called count. We initialize or start count by setting it to 0.

If you press button A, count will go up by 1.

If you press button B, count goes down by 1.

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.

注释:代码底部未使用的积木块将会在挑战赛里面用到。

挑战

The starter code is available here.

挑战1

Create a new variable, name it Adjustment, set it to 3 and put it in the on start block

查看代码

let Adjustment = 0
let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
Adjustment = 3
          

挑战2

Instead of setting Count +/- 1, set Count to +/- Adjustment

查看代码

let Adjustment = 0
let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
Adjustment = 3
          

挑战3

Use the A+B buttons pressed event handler to reset the count and then show it.

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

查看代码

let Adjustment = 0
let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {
    count = 0
    basic.showNumber(count)
})
count = 0
Adjustment = 3
          

独立练习

Create your own variable on start. Change the variable in the on shake event. Be prepared to tell a story about this variable and why it changes.

Example - My variable is called maraca. When I shake it the variable goes down by 5. This shows how sad my mother gets when I play the maraca in her ear.

问答

Use these questions to reflect on the original code found here.

let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
        

问题 1

What is a variable?

显示答案

Answers may vary but a variable is a place where you can store and retrieve data.

问题 2

What is the stored in the variable called count on start? Why are we creating the variable count in this code?

显示答案

count is 0 on start. The score changes when buttons are pressed so we need to store the value.

问题 3

Which number is stored in count after running this code and pressing button A once? Explain why you chose that number.

显示答案

We are only pressing button A once. The number on display is one. Pressing button A would add one to zero(1+0).

问题4

Which number is stored in count after running this code, pressing button A one time, and pressing button B two times? Explain why you chose to that number.

显示答案

We are only pressing button A once. The number goes up 1. Then pressing button B twice subtracts 2. -1 will be on display.(0+1-2=-1)