Counting Reps with Leo
Learn how to use loops and count variables in programs
Questions to investigate
• How can a robot count the number of times it performs a function?
• 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
(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.
(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 spike import MotorPair from spike.control import wait_for_seconds \# Initialize Motors motor_pair=MotorPair('B', 'F') \# Complete 5 sit-ups. for count in range(5): motor_pair.start(0, -30) wait_for_seconds(.5) motor_pair.stop() motor_pair.start(0, 30) wait_for_seconds(.5) motor_pair.stop()
Students may identify that the 5 included in the for count 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 “count” in the “for count in range” line of code is a counter variable. Students can assign any word or even just a letter to be the count variable. The same name will need to be used consistently.
Show students this new program that includes a print function for the counter variable named count. By adding this new line of code, students can follow the count with Leo to know how many sit-ups are completed.
from spike import MotorPair from spike.control import wait_for_seconds \# Initialize Motors motor_pair=MotorPair('B', 'F') \# Complete 5 sit-ups. for count in range(5): motor_pair.start(0, -30) wait_for_seconds(.5) motor_pair.stop() motor_pair.start(0, 30) wait_for_seconds(.5) motor_pair.stop() print(count+1) print('Whew! That was tough')
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.
(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.
(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.
from spike import MotorPair from spike.control import wait_for_seconds \# Initialize Motors motor_pair=MotorPair('B', 'F') \# Complete 5 sit-ups. for count in range(5): motor_pair.start(0, -30) wait_for_seconds(.5) motor_pair.stop() motor_pair.start(0, 30) wait_for_seconds(.5) motor_pair.stop() print(5-count) print('Whew! That was tough')
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.
(Group Exercise, 5 minutes)
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?
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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.
Supporto per l'insegnante
• 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
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.