SPIKE™ Prime with Python

Warm Up Loop with Leo

Students will learn about loops and how to program with loops.

45 min
Beginner
Years 7-9 or Key Stage 3

Questions to investigate

• How can lines of code be repeated while creating efficient programs?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Have students stand and walk in a square around their chair, desk, or the room. As they walk, ask students to describe their individual movements (step forward, step forward, turn to the right/left, etc.).

Ignite a discussion with students around creating their movement into a program. Ask student to think about the lines of code needed to create this step-by-step program. Write the steps out together as a pseudocode. Pseudocode is writing in words what you want the program to do.

Ask students if they are excited to type out so many lines of code. Discuss alternatives to make the program more efficient (i.e. a shorter program that uses less lines of code to do an action or task).

Prompt students as needed to recognize the pattern that repeats within the steps.

Introduce the concept of loops to students. Loops allow you to repeat lines of code, which shortens the program. This saves the programmer time and makes the code easier for the computer to run the program.

Explore

(Small Groups, 20 minutes)

Students will explore working with for loops using the Leo, the trainer model.
Direct students to the BUILD section in the SPIKE App. Here students can access the building instruction for Leo, the Trainer. Ask students build the model. You can also find the building instructions 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.

Start Moving
Challenge students to program Leo to complete one sit-up.

Ask students to program Leo to complete 1 sit-up. Encourage students to create the program on their own using notes and the Knowledge Base as a reference. Provide the sample code as needed if students struggle.

Sample Code:

from hub import port
import runloop
import motor_pair
async def main():

    # Pair motors on port B and F
    motor_pair.pair(motor_pair.PAIR_1, port.B, port.F)
    # Run motors one direction and then the other
    await motor_pair.move_for_time(motor_pair.PAIR_1, 500 ,0, velocity = 500)
    await motor_pair.move_for_time(motor_pair.PAIR_1, 500 ,0, velocity = -500)

runloop.run(main())

Discuss the program with students.

Ask students if they think one sit-up is enough of a workout for Leo. Prompt students to challenge Leo to complete at least 5 sit-ups. Discuss ways to change the program to repeat the same action several times.

Students might suggest copying and pasting the code five times. This is one way to do it, but not very efficient. Discuss efficiency as needed with students.

Prompt students to think about how we could use only one additional line of code to do the action. Share this code with students and see if students can identify the one new line of code. Ask students to add this line of code and run the program.

from hub import port
import motor_pair
import runloop

async def main():
    # Pair motors on port A and B
    motor_pair.pair(motor_pair.PAIR_1, port.B, port.F)

    for index in range(5):
        await motor_pair.move_for_time(motor_pair.PAIR_1, 500 ,0, velocity = 500)
        await runloop.sleep_ms(500)        
        await motor_pair.move_for_time(motor_pair.PAIR_1, 500 ,0, velocity = -500)
        await runloop.sleep_ms(500)

runloop.run(main())

Discuss the program with students.

Explain

(Whole Group, 5 minutes)

Discuss with students how the program worked.
Ask students questions like:
• How did the new program work?
• What does the “for index in range” line to tell the program?
• What is efficiency? How is using a for loop instead of copying and pasting the code more efficient?

Explain to students that this is an example of a “for loop” or repetitive statement, which allows us to repeat a portion of the code a set number of times. In this case, we repeated the code 5 times as indicated in the parentheses. Notice the lines of code following the for loop is indented. All lines of code indented after the for loop will be looped.

Elaborate

(Small Groups, 10 minutes)

Challenge students to add to their program to include something for Leo to do after his sit-ups (i.e. after the loop).

Ask students to add to their code to include either an image or a sound and to print something in the console. Leo should still complete at least 5 sit-ups, but then do something new. Remind students that anything that do not want repeated in the loop will not be indented.

Note: Students may change the power level of the motors. Changing the power level will change the way Leo moves. As time allows, consider investigating this together.

Allow students to share their final programs and discuss how they were able to add to their code. Specifically draw attention to where students use indentation and where they do not.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• How can you use a for loop to make a program more efficient? (shorter to code)
• How do you show in the program that the loop has ended?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using for loops in your program?
• 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 a program with loops.
• Build and program a sit-up machine.

• 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.