SPIKE™ Prime with Python

Counting Reps with Leo

Learn how to use loops and count variables in programs

45 min
Beginner
Years 7-9 or Key Stage 3

Questions to investigate

• How can a robot count the number of times it performs a function?

Prepare

• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.
• Ensure students have built the Leo, the Trainer model, which was used in the Warm Up Loop with Leo lesson

Engage

(Group Discussion, 5 minutes)

Have students do a light exercise for one minute. This could be jumping jacks, toe touches, or anything that allows them to repeat an action several times within the minute. Do not ask them to count how many they do.

When the minute is finished, ask students to report how many of the exercise they were able to complete. Most students will not know because you did not tell them to count.

Have a short discussion about what we kind of information we could share if we knew how any each person did of the exercise. Examples might include a total number of the reps, create a comparison like boys to girls, or create a graph or a range of highest to lowest.

Explore

(Small Groups, 20 minutes)

Students will explore working with a count variable with for loops.

Discuss with students how we can help Leo count his reps when doing his warm up exercises. Show students the sample code from the Warm Up Loop with Leo lesson to support your discussion.

Sample Code from Warm Up Loop with Leo:

from hub import port
import runloop
import motor_pair

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())

Students may identify that the 5 included in the for index in range line of code tells us how many total sit-ups Leo will complete. Prompt students to think about how we can keep count and where the count could show up.

Explain to students that the word “index” in the “for count in range” line of code is a variable. Students can assign any word or even just a letter to be the variable. This variable “counts” the number of times the code loops. The first loop is 0, then 1 and so on. The loop stops when it reaches the number in the parenthesis. In this case it is 5. So, the index counts 0, 1, 2, 3, 4. That makes 5 loops. The same name will need to be used consistently.
Show students this new program that includes a print function for the variable named index. By adding this new line of code, students can follow the “count” with Leo to know how many sit-ups are completed.

from hub import port
import runloop
import motor_pair

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)

        # The +1 is added since index starts at 0
        print(index + 1)

    print('Whew! That was tough.')

runloop.run(main())

Ask students to add these new lines of code into their program if they have the program saved from the Warm Up Loop with Leo Lesson. Otherwise, students will need to type this code in. Ensure students have their console open to watch the count.

Explain

(Whole Group, 5 minutes)

Discuss the program with students. Identify how the print function is working to show a count of the sit-ups that Leo completes.

Ask students questions like:
• How did the program work?
• What is the counter variable and how does it work?
• What is the purpose of the line print(count+1)?
• Why is the last line “Whew! That was tough” not printed five times in the console?

Ensure students are comfortable with the counter variable, understanding that any amount can be assigned to the variable. The counter variable can be used in a variety of ways in the code including to assign the number of times the program loops as well as being used in the print function to show the value.

Elaborate

(Small Groups, 10 minutes)

Challenge students to create a countdown for Leo.
Explain to students that sometime when exercising we count down to one instead of counting up. Ask students to change their code to count from five to one and then play a fun sound at the end to show they are finished.
Sample Code:

from hub import port
import runloop
import motor_pair

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)

        # The +1 is added since index starts at 0
        print(5 - index)

    print('Whew! That was tough.')


runloop.run(main())

Allow students additional time to explore how to use the counter variable in their program. Prompt students with ideas like how to add something motivational after each rep or count by 5’s.

Allow students to share their final programs. Discuss different ways that students used their counter variable.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students. Ask students questions like:
• What are different ways that you could use the counter variable?
• How can a loop be embedded or included in a program?
• After the loop is completed, how can the program continue?
• What are different ways you could use a for loop in programs?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using a counter variable?
• 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:
• Program a sit-up machine to count the reps and to complete a count down.

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