SPIKE™ Prime with Python

Mind Games

Use and compare multiple lists

90-120 min.
Middels
6.–8. trinn

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, violet, 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 spike import PrimeHub, App, Motor, ColorSensor 
from spike.control import wait_for_seconds

hub = PrimeHub()
app = App()

mouth_motor = Motor('A') 
color_sensor = ColorSensor('B')

candy1 = [ ] 
candy2 = [ ] 

while True:
\# This makes the Game Master eat the candy stick then read and record its sequence of colors in the list called `candy1`
if hub.left_button.is_pressed():
     hub.light_matrix.off() 
     candy1.clear()
     mouth_motor.set_default_speed(-50) 
     mouth_motor.run_for_seconds(2)
     app.play_sound('Bite')
     app.play_sound('Bite')
for x in range(5): 
     candy1.append(color_sensor.get_color())
     wait_for_seconds(1) 
     mouth_motor.set_default_speed(50)
     mouth_motor.run_for_degrees(95)
     wait_for_seconds(1) 
if hub.right_button.is_pressed():
\# This makes the Game Master eat the candy stick then read and record its sequence of colors in the list called `candy2`
     candy2.clear() 
     mouth_motor.set_default_speed(-50) 
     mouth_motor.run_for_seconds(2) 
     app.play_sound('Bite')
     app.play_sound('Bite')
     for x in range(5): 
          candy2.append(color_sensor.get_color())
          wait_for_seconds(1) 
          mouth_motor.set_default_speed(50) 
          mouth_motor.run_for_degrees(95)
          wait_for_seconds(1)
\# Light up the position of the red bricks if it is in the same position in both of the candy sticks.
     candy1_red_index = candy1.index('red') 
     candy2_red_index = candy2.index('red') 
     for x in range(5):
          print(candy1[x])
     if candy1_red_index == candy2_red_index:
          for x in range(5):
          hub.light_matrix.set_pixel(x, candy1_red_index)
          app.play_sound('Win') 
     else:
          app.play_sound('Oops')

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 spike import PrimeHub, App, Motor, ColorSensor
from spike.control import wait_for_seconds

hub = PrimeHub()
app = App()
mouth_motor = Motor('A')
color_sensor = ColorSensor('B')

candy1 = []
candy2 = []

while True:
\# This makes the Game Master eat the candy stick then read and record its sequence of colors in the list called `candy1`
     if hub.left_button.is_pressed():
          turns = 5
          hub.light_matrix.off()
          candy1.clear()
          mouth_motor.set_default_speed(-50)
          mouth_motor.run_for_seconds(2)
          app.play_sound('Bite')
          app.play_sound('Bite')


          for x in range(5):
               candy1.append(color_sensor.get_color())
               wait_for_seconds(1)
               mouth_motor.set_default_speed(50)
               mouth_motor.run_for_degrees(95)
               wait_for_seconds(1)
          while turns > 0:
               if hub.right_button.is_pressed():
                    turns = turns – 1
                    hub.light_matrix.off()
\# This makes the Game Master eat the candy stick then read and record its sequence of colors in the list called `candy2`
                    candy2.clear()
                    mouth_motor.set_default_speed(-50)
                    mouth_motor.run_for_seconds(2)
                    app.play_sound('Bite')
                    app.play_sound('Bite')

                    for x in range(5):
                         candy2.append(color_sensor.get_color())
                         wait_for_seconds(1)
                         mouth_motor.set_default_speed(50)
                         mouth_motor.run_for_degrees(95)
                         wait_for_seconds(1)

\# Light up the position of the red bricks if it is in the same position in both of the candy sticks.
                    candy1_red_index = candy1.index('red')
                    candy2_red_index = candy2.index('red')
                    candy1_blue_index = candy1.index('blue')
                    candy2_blue_index = candy2.index('blue')
                    candy1_green_index = candy1.index('green')
                    candy2_green_index = candy2.index('green')
                    candy1_yellow_index = candy1.index('yellow')
                    candy2_yellow_index = candy2.index('yellow')
                    candy1_violet_index = candy1.index('violet')
                    candy2_violet_index = candy2.index('violet')

                    for x in range(5):
                         print(candy1[x])
                    if candy1_red_index == candy2_red_index:
                         app.play_sound('Win')
                         for x in range(5):
                             hub.light_matrix.set_pixel(x,candy1_red_index)
                    if candy1_blue_index == candy2_blue_index:
                         app.play_sound('Win')
                         for x in range(5):
                             hub.light_matrix.set_pixel(x, candy1_blue_index)
                   if candy1_green_index == candy2_green_index:
                         app.play_sound('Win')
                         for x in range(5):
                                      hub.light_matrix.set_pixel(x, candy1_green_index)
                   if candy1_yellow_index == candy2_yellow_index:
                         app.play_sound('Win')
                         for x in range(5):
                             hub.light_matrix.set_pixel(x, candy1_yellow_index)
                   if candy1_violet_index == candy2_violet_index:
                         app.play_sound('Win')
                         for x in range(5):
                             hub.light_matrix.set_pixel(x, candy1_violet_index)

                  \#This checks to see if all indexes match
if candy1_red_index == candy2_red_index and candy1_blue_index == candy2_blue_index and candy1_green_index == candy2_green_index and candy1_yellow_index == candy2_yellow_index and candy1_violet_index == candy2_violet_index:
                   app.play_sound('Triumph')

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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.

Lærerstøtte

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.

CookiesLegal NoticePrivacy Policy

LEGO, the LEGO logo, the Minifigure, DUPLO, the SPIKE logo, MINDSTORMS and the MINDSTORMS logo are trademarks and/or copyrights of the LEGO Group. ©2022 The LEGO Group. All rights reserved. Use of this site signifies your agreement to the terms of use.