SPIKE™ Prime with Python

Listing Letters

Create a list and display letters on the hub

45 min
Poziom podstawowy
Klasy 4–8

Questions to investigate

• Why would we need to provide more than one value for a variable?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Engage students in a conversation about lists.

Spark a discussion with students about ways that they use lists or see others use lists. Ask students to list types of lists (such as a grocery list, a to-do list, a roster or list of players).
Ask students to think about:
• Why do we use lists?
• What does the list provide us?
• How do we reference items on a list?
• How can we organize lists?

As time allows, challenge students to create different types of lists and then share the lists seeing if others have items or elements to add to the list. Types of lists could include: types of fruit or food, colors, types of places to live, types of music, etc.

Explore

(Small Groups, 20 minutes)

Students will investigate how to create lists through an unplugged activity.

Creating Lists: Unplugged
Direct students to take all the 2x4 bricks from the set. Each person will have 5 bricks (yellow, green, violet, red, and blue) and needs to have 5 small pieces of paper or sticky notes with one number from 1-5 written on each.

Ask students to list or name each color that they have represented by the 5 bricks from their set. Students should each identify yellow, green, violet, red, and blue as their colors.
Explain to students that these are the values of their list. A list is a variable that can store multiple values. To program a list in python, students will need to provide a name to the list, list the items or elements included, and then be aware of the position or index value of each element in the list.

Ask students to each arrange their five bricks on one of the numbered pieces of paper or sticky notes. There is no particular order that they have to follow. Ask each person to write out the full list in the order they have chosen including the numbered position or index value of each brick in the list.

Round 1
Working together, have students play a game to create a stack of bricks using the different indexes in their lists.

Each partner will randomly select a number from a bag, bowl, or similar object holding the slips of paper or sticky notes from their own list. The number, representing the index in the list, will provide the order in which to stack their bricks. Each student should have a stack of 5 bricks in the end.

After the partners create their stacks, have them compare the color patterns. Ask students if the color patterns are the same or different. Encourage them to discuss why.

Round 2
Ask students to now place both sets of paper or sticky notes into the bag, bowl or similar object. Now they will work together to create a stack of 5 bricks using both sets of papers, which will allow for repeated items. Have the students select 1 of the two lists to follow and then repeat the stacking game.

Once students have finished, have them examine their new pattern. Ask students to identify if any colors are repeated. Discuss why this happened.

Programming Lists: Listing Letters
Students will program the hub to display a random letter from the alphabet.

Direct students to obtain the hub from their SPIKE Prime set. Then ask 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.

Discuss with students how to create a list in python. Show students the sample code to support your discussion. Ask students to type this code into their programming canvas and run the program.

Sample code:

from spike import PrimeHub, LightMatrix
import random 

hub = PrimeHub() 
letters = list()

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

hub.light_matrix.write(letters[4])

Students should see the letter D printed on their hub. Discuss the program together.

Explain

(Whole Group, 5 minutes)

Discuss with students how their lists and program worked.
Ask students questions like:
• How did we create a list during our unplugged activity? How did we use the list to create our stacks?
• How can we program a list in python?
• What determined the letter shown on the hub?
• How could you make a random letter appear on the hub?

Review lists and their components again with students to ensure they understand what is needed to create a list.

Elaborate

(Small Groups, 10 minutes)

Challenge students to a game of Brick-tionary using their lists to randomly generate a letter that their item need to begin with.

Each group will need to modify their code to allow for a random letter to be generated when the left button is pushed. Introduce the random.choice line of code to students and discuss how this is generating a random letter for them.

Sample code:

from spike import PrimeHub, LightMatrix
import random 

hub = PrimeHub() 
letters = list()

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

hub.left_button.wait_until_pressed()
hub.light_matrix.write(random.choice(letters))

Working in groups of 4, allow students to play several rounds of the game. Each student will take a turn playing the Brain Game Master, whose job is to run the program to randomly generate a letter. Each of the other students will then have 2 minutes to build an item that starts with that letter using bricks from their sets. The Brain Game Master will then try to guess what each item is at the end.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when we created the random letter generator?
• How can lists be used?
• What is important to remember when creating lists in Python?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using 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.

Wsparcie dla nauczyciela

Students will:
• Create and utilize lists.
• Code with compound conditionals using lists.

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

CSTA
2-CS-01 Recommend improvements to the design of computing devices, based on an analysis of how users interact with the devices.
2-DA-09 Refine computational models based on the data they have generated.
2-AP-10 Use flowcharts and/or pseudocode to address complex problems as algorithms.
2-AP-12 Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
2-AP-18 Distribute tasks and maintain a project timeline when collaboratively developing computational artifacts.
2-AP-16 Incorporate existing code, media, and libraries into original programs, and give attribution.
2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.

Legal NoticePrivacy PolicyCookies

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.