SPIKE™ Prime with Python

Mind Games

Use and compare multiple lists

90-120 min.
Intermed.
Grades 6-8

Questions to investigate

• How can indexing compare two lists for common components?

Prepare

• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.

Engage

(Group Activity, 15 minutes)

Engage students in a conversation about stretching their mind to think about how different lists are similar.

Ask students to remove all 10 of the 2x4 colored bricks in their set. Ask each student in the group to take 1 of each color including red, magenta, yellow, blue, and green.

Partner 1 should create a stack of their 5 bricks in any order without showing Partner 2. Ask Partner 1 to describe the build to Partner 2 in order to create the same stack (i.e. show the build, tell the order without showing, etc.). Once the pair thinks the stacks are the same, they are finished.

Ask the partners to switch and do the exercise again. This time, the partners have to come up with a new way to communicate about the stack (i.e. different than their first approach which could be describing in a different way, not using words, etc.).

After groups complete several rounds, discuss together as a class how the game is played and ask students to write a pseudocode program for this game.

Explore

(Small Groups, 45 minutes)

Students will investigate programs that include multiple lists and how to locate a given position or index value in the list.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for Game Master model.  Ask students to build the model. The building instructions are also available at https://education.lego.com/en-us/support/spike-prime/building-instructions.   

Direct students to open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.  

Students will use the Game Master model to read the colors of the candy stick bricks. Provide students with the sample program below. Review the program together to discuss what the program does. Ask students to run the program and then feed one of the candy sticks into the model.

Sample Program:

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():
    # create lists
    candy1 = []
    candy2 = []

    # this makes the Game Master eat candy stick 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')

    # this will read and record its sequence of colors in the list called 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)

    # this makes the Game Master eat candy stick 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')

    # this will read and record its sequence of colors in the list called 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)

    # light up the position of red bricks if it is in the same position in both candy sticks
   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())

Allow students time to investigate the program.

Explain

(Whole Group, 15 minutes)

Discuss with students how the program worked.
Ask students questions like:
• How did the program work?
• How is this program using lists?
• How does this program index and compare the two lists?

Elaborate

(Small Groups, 25 minutes)

Challenge students to create a new program that indexes and compares all of the colors to check for identical block stacks.

Discuss with students how to modify their program in order to check for the other four colors.
1. Need to index the other colors
2. Need to compare the lists for the other colors
3. Need to set the pixels for the other colors if both are in identical location on the stack.

Sample Program:

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():
    # create lists
    candy1 = []
    candy2 = []

    # this makes the Game Master eat candy stick 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')

    # this will read and record its sequence of colors in the list called 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)

    # this makes the Game Master eat candy stick 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')

    # this will read and record its sequence of colors in the list called 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)

    # light up the position of red bricks if it is in the same postion in both candy sticks
    # the number correlates to the color
    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())

Allow students time to investigate the program.

Evaluate

(Group Exercise, 15 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when you elaborated on the original program?
• How did the program index and compare lists?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using indexing to compare lists?
• What characteristics of a good teammate did I display today?
• Ask students to rate themselves on a scale of 1-3, on their time management today.
• Ask students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

Students will:
• Create two lists in one program
• Compare two lists within the program

• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals

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.