# Course F - Variables 1 - Variables With A Counter

Students create programs with events, variables, and conditionals.

• 대상 연령 10+
• 90 예상 시간(분)
• MakeCode Editor

## 소개

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.

마이크로비트 코스 레슨들은 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.

## 활동

준비물

• BBC 마이크로비트 1대
• USB 케이블 1개
• AAA 배터리 팩 (옵션)
• AAA 배터리 2개 (옵션)

### 수업 자료

``````
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)
``````

### 수(정수) 출력

LED 스크린 화면에 수(정수)를 출력합니다.

``````
let item = 0
item = 0
``````

### 값 저장

등호 기호를 사용하면, 변수에 어떤 수나 문자열을 저장할 수 있습니다.

4 단계

## 따라해보기

마이크로비트를 위한 단계별 설명이나 온라인 코드를 제공할 예정입니다. 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.

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

View code
``````
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
``````

### 도전과제 2

View code
``````
let count = 0
input.onButtonPressed(Button.A, () => {
basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
``````

### 도전과제 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.

View code
``````
let count = 0
input.onButtonPressed(Button.A, () => {
basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {
count = 0
basic.showNumber(count)
})
count = 0
``````

## 자기주도 연습

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.