활동

# Which way now?

초급 | MakeCode, Python | LED, 가속도 센서 | 난수, 변수, 선택 실행, 성능 평가 도구

## 1단계: 만들어 보세요.

### 프로젝트 소개

Shake your micro:bit and be given a random direction to walk. You will learn about variables, and using random numbers, selection and comparison logic blocks.

다음 동영상을 통해 만들고 프로그래밍하는 방법을 살펴보세요.:

### 설명

• The program uses a variable called “direction” to store a random number.
• Variables store numbers or values that can change in a computer program.
• Shake the micro:bit to set “direction” to a random number between one and three.
• The program then uses selection and comparison logic blocks to test the value of the variable.
• If “direction” is equal to 1, then an arrow pointing north shows on the micro:bit’s LED display. If “direction” is equal to 2, then the arrow points east. Otherwise, the arrow points west.
• To make the battery last longer, the program pauses for half a second after the arrow appears then clears the screen.

### 준비물

• micro:bit (또는 MakeCode 시뮬레이터)
• MakeCode 편집기
• 배터리 팩(선택 사항, 있으면 좋음)

## 2단계: 프로그래밍 해보세요.

``````1# Imports go at the top
2
3
4
5from microbit import *
6import random
7
8while True:
9    if accelerometer.was_gesture('shake'):
10        random_number = random.randint(1, 3)
11        if random_number == 1:
12            display.show(Image.ARROW_N)
13        elif random_number == 2:
14            display.show(Image.ARROW_E)
15        else:
16            display.show(Image.ARROW_W)
17    sleep(2000)
18    display.clear()``````