SPIKE™ Prime with Python

Jumping for Lists

Create a program using lists to capture data of physical performance

90-120 min.
Intermed.
Years 7-9 OR Key Stage 3

Questions to investigate

• How can you create a list that represents your personal statistics for performance?

Prepare

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

Engage

(Group Activity, 5 minutes)

Engage students in a conversation about the motion of your body when you do a squat. (For example, your legs bend, and your torso gets closer to the ground.) Have students stand up and do a squat.

Next, have students jump in place straight up. Ask students what body movements were made doing the jump.

What are ways to measure how low a body gets during a squat or how high a body is at the apex of a jump?

Explore

(Small Groups, 45 minutes)

Students will investigate ways to generate data to include in lists.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for Smart Kettlebell 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.  

Part 1: Create data for your list
Students should program their Kettlebell model to provide several heights for their jumps. Students will use this information to create a list to use later.

Review the sample program with students. Discuss the way that the force and distance sensors are used in this program. Students will use the force sensor to indicate when the jump happens and the distance sensor to measure the height of the jump. Ask students to run the program and see what values they get for their heights.

Sample Program:

from hub import port, light_matrix
import runloop
import distance_sensor
import force_sensor

def is_force_sensor_pressed():
    # collect input from force sensor
    return force_sensor.pressed(port.B)

def is_force_sensor_released():
    # collect input from force sensor
    return not force_sensor.pressed(port.B)

async def main():
    runloop.until(is_force_sensor_pressed)
    await light_matrix.write('JUMP!')

    runloop.until(is_force_sensor_released)
    dist_cm = distance_sensor.distance(port.F)/10
    print(str(dist_cm) + ' cm')

runloop.run(main())

Note: students should press the force sensor, hold it, jump, and then release the sensor when they are at the height of their jump. The distance sensor may not read properly when looking at some carpet floors. If the jump reads –0.1 cm, try a different location.

After students explore the program, ask each person on the team to complete three jumps. Students should record the values for each jump in a table.

Example Table:

Unit_10_Lesson_4_Table.jpg

Part 2: Create your list
Students will create two lists in a new program using the data they generated in part 1.
Discuss with students how to create a new program that will include a list for each student and the data from each of their jumping trials as the values in the list. Remind students of the importance of naming variables and lists to easily tell them apart as they program. Here students might use their own names for their individual lists.

Ask students to create their program including both lists, indicating what the maximum value from each list is and then sharing each value on the hub. Introduce the max(name1) line of code to students to indicate their max value in their list.

Sample program (Students should use their own names and data from their table):

from hub import port, light_matrix
import runloop
import force_sensor

def is_force_sensor_pressed():
    # collect input from force sensor
    return force_sensor.pressed(port.B)

async def main():

    # create a list for each person using the values from the table
    name1 = [trial1, trial2, trial3]
    name2 = [trial1, trial2, trial3]

    # print the maximum value from each person's list
    print('Name 1: ' + str(max(name1)))
    print('Name 2: ' + str(max(name2)))
    
    # display the max values on the hub when the force sensor is pressed
    await runloop.until(is_force_sensor_pressed)
    await light_matrix.write(str(max(name1)))
    await runloop.sleep_ms(2000)
    light_matrix.show_image(light_matrix.IMAGE_YES)
    await runloop.sleep_ms(1000)
    await light_matrix.write(str(max(name2)))
    await runloop.sleep_ms(2000)
    light_matrix.show_image(light_matrix.IMAGE_YES)

runloop.run(main())

Allow students time to investigate the program trying different ways to show the output or the max value for each list.

Part 3: Putting lists together
Students will modify their program to add their two lists together to make one list.
Explain to students that concatenating lists allows two lists to be combined into one using the + sign. Share with students the example line of code list3 = list1 + list 2 as a way to add lists together. Challenge students to concatenate their lists and find the new max or highest number from both lists.

Sample program:

from hub import port, light_matrix
import runloop
import force_sensor

def is_force_sensor_pressed():
    # collect input from force sensor
    return force_sensor.pressed(port.B)

async def main():
    # create a list for each person using the values from the table
    name1 = [trial1, trial2, trial3]
    name2 = [trial1, trial2, trial3]

    # print the maximum value from each person's list
    print('Name 1: ' + str(max(name1)))
    print('Name 2: ' + str(max(name2)))

    # combine the lists when the force sensor is pressed
    # display the sum of the new list
    await runloop.until(is_force_sensor_pressed)
    combo_list = name1 + name2
    print(combo_list)
    await light_matrix.write(str(sum(combo_list)))
    print(sum(combo_list))
    light_matrix.show_image(light_matrix.IMAGE_YES)

runloop.run(main())

Allow students time to investigate the program trying different ways to show the output of the sum value for the combined list. Students might also try showing the sum of each list as well as the combined sum.

Explain

(Whole Group, 15 minutes)

Discuss with students how the program worked.
Ask students questions like:
• How were you able to generate your own data to include in a list?
• How were you able to incorporate using the force sensor as a button to push for an action to happen?
• How were you able to find different information about your lists (such as sum or max)? What other values do you think you could find?
• How can you combine lists together? When might this be useful?

Elaborate

(Small Groups, 25 minutes)

Challenge students to indicate if a certain number is included in their list.

Discuss with students how to look for information within their lists. While students can easily see the list in their program, there are times when you might need to investigate what has been included in a list. Discuss ways and reasons this could happen as a group.

Ask students to modify their program that has the combo list to identify if the number 20 is included in the combo list. Note: select an appropriate number according to students' data.
Share the sample program with students and have them identify how the program is checking the list to see if the number is included.

Sample program:

from hub import port, light_matrix
import runloop
import force_sensor

def is_force_sensor_pressed():
    # collect input from force sensor
    return force_sensor.pressed(port.B)

async def main():
    # create a list for each person using the values from the table
    name1 = [trial1, trial2, trial3]
    name2 = [trial1, trial2, trial3]

    # print the maximum value from each person's list
    print('Name 1: ' + str(max(name1)))
    print('Name 2: ' + str(max(name2)))

    # combine the lists when the force sensor is pressed
    # display the sum of the new list
    await runloop.until(is_force_sensor_pressed)
    combo_list = name1 + name2
    print(combo_list)
    await light_matrix.write(str(sum(combo_list)))
    print(sum(combo_list))
    light_matrix.show_image(light_matrix.IMAGE_YES)
    await runloop.sleep_ms(2000)
    light_matrix.clear()

    if (20 in combo_list):
        print('The number 20 is in the list.')
        light_matrix.show_image(light_matrix.IMAGE_YES)
    else:
        print('The number 20 is not in the list')
        light_matrix.show_image(light_matrix.IMAGE_NO)

runloop.run(main())

Ask students to modify their program similar to the sample program and search for various values to see what is included or not in the program. Students can modify the program in additional ways to indicate if it is or is not included.

Evaluate

(Group Exercise, 10 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What are some ways that you can see using multiple lists?
• What are some ways that lists can benefit you in a program?
• What are different types of information you can take from your lists?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using multiple 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 data from the force and distance sensors to use in a list
• Program a list based on the data gathered from the jumping trials (i.e., height of jumps)

• 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-11 Create clearly named variables that represent different data types and perform operations on their values.
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.