SPIKE™ Prime with Python

AI 개념의 시작: 마인드 게임

여러 개의 리스트를 사용하고 서로 비교합니다

90-120분
중급
초3-중3학년

탐색을 위한 질문

• 인덱싱을 사용하여 두 리스트의 공통 요소를 비교하려면 어떻게 해야 하나요?

준비

• 스파이크 프라임 허브가 충전되어 있는지 확인하세요(특히 블루투스로 연결되어 있는 경우).

관심유도

(그룹 활동, 15분)

학생들의 생각을 좀 더 확장하는 차원에서, 여러 리스트를 비교하여 얼마나 비슷한지를 알아내는 방법에 대해 학생들과 대화를 나눠보세요.

학생들에게 세트의 2x4 컬러 브릭 10개를 모두 꺼내게 하고, 그룹의 모든 학생이 저마다 빨간색, 자홍색, 노란색, 파란색, 녹색을 각각 하나씩 지니게 하세요.

이제 파트너 1이 파트너 2에게 보여주지 않은 채로 브릭 5개를 아무 순서로나 겹쳐 쌓습니다. 그 다음, 파트너 1이 설명해주는 순서에 따라 파트너 2가 브릭을 쌓습니다(즉, 조립 방법을 보여주거나, 보여주지 않은 채로 순서를 말해주는 등). 이런 식으로 둘이 생각하기에 조립 모델이 같아졌을 것이라고 판단될 때까지 진행을 계속합니다.

이어 두 파트너가 서로 역할을 바꾸고 실습을 재개하되, 이번에는 브릭을 쌓는 순서를 전해줄 새로운 방법을 찾아내야 합니다(즉, 첫 번째의 경우와 다르게, 단어를 사용하지 않고 다른 방법으로 설명을 할 수 있어야 함).

이렇게 그룹별로 여러 차례 게임을 해본 후, 게임이 어떻게 진행되었는지에 대해 학급 전체 토의를 진행하고, 학생들에게 이 게임의 유사 코드 프로그램을 작성해보라고 하세요.

탐구

(소규모 그룹, 45분)

학생들이 여러 개의 리스트가 포함된 프로그램을 살펴보고, 리스트에서 특정 위치 또는 인덱스 값을 찾아내는 방법을 조사합니다.

학생들에게 스파이크 앱의 조립 섹션을 찾아보라고 하세요. 게임 마스터 모델의 조립 가이드가 제시되어 있을 것입니다.  학생들에게 모델을 조립하게 하세요. 조립 가이드는 https://education.lego.com/en-us/support/spike-prime/building-instructions 페이지에서도 볼 수 있습니다.   

학생들에게 파이썬 프로그래밍 캔버스에서 새 프로젝트를 열게 하세요. 혹시 프로그래밍 영역에 이미 코드가 들어 있다면 일단 그것부터 다 지우고, 허브를 연결하게 하세요.  

이제 학생들이 게임 마스터 모델을 사용하여 사탕막대 브릭의 색상을 읽을 차례입니다. 학생들에게 아래의 샘플 프로그램을 제시하고, 프로그램을 함께 검토하고, 프로그램이 무슨 일을 하는지를 토의에 부치세요. 뒤이어 학생들에게 프로그램을 실행한 다음, 사탕막대 하나를 모델에게 먹여줘보라고 하세요.

샘플 프로그램:

from hub import light_matrix, button, port
import runloop
import motor
import color_sensor
import color
from app import sound

def feed_candy_1():
    return button.pressed(button.LEFT)

def feed_candy_2():
    return button.pressed(button.RIGHT)

async def main():
    # 목록을 만듭니다
    candy1 = []
    candy2 = []

    # 게임 마스터가 사탕막대 1을 먹습니다
    await runloop.until(feed_candy_1)
    light_matrix.clear()
    candy1.clear()
    await motor.run_for_time(port.A, 2000, -500)
    await sound.play('Bite')
    await sound.play('Bite')

    # candy 1이라는 이름의 리스트에 들어 있는 색상의 순서를 읽고 기록합니다
    for x in range(5):
        candy1.append(color_sensor.color(port.B))
        await runloop.sleep_ms(1000)
        await motor.run_for_degrees(port.A, 95, 500)

    # 게임 마스터가 사탕막대 2를 먹습니다
    await runloop.until(feed_candy_2)
    light_matrix.clear()
    candy2.clear()
    await motor.run_for_time(port.A, 2000, -500)
    await sound.play('Bite')
    await sound.play('Bite')

    # candy 2라는 이름의 리스트에 들어 있는 색상의 순서를 읽고 기록합니다
    for x in range(5):
        candy2.append(color_sensor.color(port.B))
        await runloop.sleep_ms(1000)
        await motor.run_for_degrees(port.A, 95, 500)

    # 두 사탕막대 안에 자리한 위치가 같을 경우 빨간색 브릭의 위치에 불을 켭니다
   candy1_red_index = -1
    If color.RED in candy1:
        candy1.index = candy1.index(color.RED)
    candy2_red_index = -1
    If color.RED in candy2:        candy2_red_index = candy2.index(Color.RED)

    for x in range(5):
        print(candy1[x])

    if candy1_red_index == candy2_red_index:
        for x in range(5):
            light_matrix.set_pixel(x, candy1_red_index, 100)
            await sound.play('Win')

    else:
        light_matrix.show_image(light_matrix.IMAGE_NO)
        await sound.play('Oops')
                
runloop.run(main())

학생들에게 시간을 주고 프로그램을 살펴보게 하세요.

설명

(전체 그룹, 15분)

프로그램이 어떻게 작동했는지에 대해 학생들과 이야기를 나눠보세요.
이런 질문을 던져보세요.
• 프로그램이 어떻게 작동했나요?
• 이 프로그램에서 리스트가 어떻게 사용되었나요?
• 이 프로그램이 두 개의 리스트를 어떻게 인덱싱하고 비교하나요?

다듬기

(소규모 그룹, 25분)

학생들에게 모든 색상을 인덱싱하고 비교하여 동일한 블록 순서를 확인하기 위한 새로운 프로그램을 만들게 하세요.

다른 네 가지 색상도 확인할 수 있도록 프로그램을 수정할 방법에 대해 학생들과 토의를 진행하세요.
1. 기타 색상들을 인덱싱해야 함
2. 기타 색상별로 리스트를 서로 비교해야 함
3. 만일 어느 항목이 두 리스트의 동일한 위치에 있을 경우 해당 색상의 픽셀을 설정해야 함

샘플 프로그램:

from hub import light_matrix, button, port
import runloop
import motor
import color_sensor
from app import sound

def feed_candy_1():
    return button.pressed(button.LEFT)

def feed_candy_2():
    return button.pressed(button.RIGHT)
    
async def main():
    # 목록을 만듭니다
    candy1 = []
    candy2 = []

    # 게임 마스터가 사탕막대 1을 먹습니다
    await runloop.until(feed_candy_1)
    light_matrix.clear()
    candy1.clear()
    await motor.run_for_time(port.A, 2000, -500)
    await sound.play('Bite')
    await sound.play('Bite')

    # candy 1이라는 이름의 리스트에 들어 있는 색상의 순서를 읽고 기록합니다
    for x in range(5):
        candy1.append(color_sensor.color(port.B))
        await runloop.sleep_ms(1000)
        await motor.run_for_degrees(port.A, 95, 500)

    # 게임 마스터가 사탕막대 2를 먹습니다
    await runloop.until(feed_candy_2)
    light_matrix.clear()
    candy2.clear()
    await motor.run_for_time(port.A, 2000, -500)
    await sound.play('Bite')
    await sound.play('Bite')

    # candy 2라는 이름의 리스트에 들어 있는 색상의 순서를 읽고 기록합니다
    for x in range(5):
        candy2.append(color_sensor.color(port.B))
        await runloop.sleep_ms(1000)
        await motor.run_for_degrees(port.A, 95, 500)

    # 두 사탕막대의 빨간색 브릭 위치가 같을 경우
    # 빨간색 브릭이 자리한 위치에 불을 켭니다
    candy1_red_index = candy1.index(color.RED) if color.RED in candy1 else -1
    candy2_red_index = candy2.index(color.RED) if color.RED in candy1 else -1
    candy1_yellow_index = candy1.index(color.YELLOW) if color.YELLOW in candy1 else -1
    candy2_yellow_index = candy2.index(color.YELLOW) if color.YELLOW in candy1 else -1
    candy1_green_index = candy1.index(color.GREEN) if color.GREEN in candy1 else -1
    candy2_green_index = candy2.index(color.GREEN) if color.GREEN in candy1 else -1
    candy1_magenta_index = candy1.index(color.MAGENTA) if color.MAGENTA in candy1 else -1
    candy2_magenta_index = candy2.index(color.MAGENTA) if color.MAGENTA in candy1 else -1
    candy1_blue_index = candy1.index(color.BLUE) if color.BLUE in candy1 else -1
    candy2_blue_index = candy2.index(color.BLUE) if color.BLUE in candy1 else -1

    for x in range(5):
        print(candy1[x])
    if candy1_red_index == candy2_red_index:
        for x in range(5):
            light_matrix.set_pixel(x, candy1_red_index, 100)
            await sound.play('Win')

    if candy1_yellow_index == candy2_yellow_index:
        for x in range(5):
            light_matrix.set_pixel(x, candy1_yellow_index, 100)
            await sound.play('Win')

    if candy1_green_index == candy2_green_index:
        for x in range(5):
            light_matrix.set_pixel(x, candy1_green_index, 100)
            await sound.play('Win')

    if candy1_magenta_index == candy2_magenta_index:
        for x in range(5):
            light_matrix.set_pixel(x, candy1_magenta_index, 100)
            await sound.play('Win')

    if candy1_blue_index == candy2_blue_index:
        for x in range(5):
            light_matrix.set_pixel(x, candy1_blue_index, 100)
            await sound.play('Win')

    if candy1_red_index == candy2_red_index and candy1_yellow_index == candy2_yellow_index and candy1_green_index == candy2_green_index and candy1_magenta_index == candy2_magenta_index and candy1_blue_index == candy2_blue_index:
        await sound.play('Triumph')

runloop.run(main())

학생들에게 시간을 주고 프로그램을 살펴보게 하세요.

평가

(그룹 실습, 15분)

교사 관찰:
학생들과 프로그램에 대해 토의를 진행하세요.
이런 질문을 던져보세요.
• 원래의 프로그램을 상세히 설명하는 과정에서 어떤 일이 일어났나요?
• 프로그램이 리스트를 어떻게 인덱싱하고 비교했나요?

자기 평가:
학생들에게 다음 질문에 대한 답을 기록장에 적으라고 하세요.
• 오늘 인덱싱을 이용해 리스트를 비교하는 방법에 대해 무엇을 배웠는가?
• 좋은 팀원의 특성은 무엇이며, 오늘 내가 그중 몇 가지를 보여주었는가?
• 학생들에게 오늘의 시간 관리에 대해 1-3점 척도로 자기 평가를 해보라고 하세요.
• 학생들에게 오늘의 재료(부품) 관리에 대해 1-3점 척도로 자기 평가를 해보라고 하세요.

교사 지원

학생 과제:
• 하나의 프로그램에서 두 개의 리스트를 만듭니다
• 프로그램 내에서 두 개의 리스트를 비교합니다

• 학생이 즉각 사용 가능한 상태의 스파이크 프라임 세트
• 스파이크 앱이 설치된 장치
• 학생용 기록장

CSTA
2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-10 Use flowcharts and/or pseudocode to address complex problems as algorithms
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-16 Incorporate existing code, media, and libraries into original programs, and give attribution.
2-AP-17 Systematically test and refine programs using a range of test cases.
2-AP-19 Document programs in order to make them easier to follow, test, and debug.