# 猜数字

Discover variables and inputs with this MakeCode activity!

• 年龄 8+
• 30 分钟
• MakeCode Editor

## 介绍

Second project using the MakeCode editor and introduces the students to the use of variables, user inputs and random numbers. When the button is pressed a random number between a given range is shown.

## 课堂活动

5个步骤

### 第一步

In the MakeCode editor select the Input option from the block menu and find the “On button pressed” block and drag onto the workspace.

``````
input.onButtonPressed(Button.A, function () {

})
``````

### 第三步

``````
let item = 0
input.onButtonPressed(Button.A, function () {
item = 0
})
``````

``````
let X = 0
input.onButtonPressed(Button.A, function () {
X = 0
})
``````

### 第四步

The next element is to build a random number element within the program. From the block menu select the “Math” option and select “pick random 0 to 10”

``````
let X = 0
input.onButtonPressed(Button.A, function () {
X = Math.randomRange(0, 10)
})
``````

As we would like the random number to be in the range from 0 to 9, click the “10” and replace it with “9”.

``````
let X = 0
input.onButtonPressed(Button.A, function () {
X = Math.randomRange(0, 9)
})
``````

### 第五步

``````
let X = 0
input.onButtonPressed(Button.A, function () {
X = Math.randomRange(0, 9)
basic.showNumber(0)
})
``````

``````
let X = 0
input.onButtonPressed(Button.A, function () {
X = Math.randomRange(0, 9)
basic.showNumber(X)
})
``````

## 挑战

• 当一个“B”按钮按下就能够清屏。
• 添加一个随机选区数字的动画。
• 开发一个动画，使其减慢随机数选择的速度。