Dance Loop with Coach
Students will practice using for loops in their programs to make them more efficient
Questions to investigate
• What are ways to make programs more efficient?
Prepare
• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.
Engage
(Group Discussion, 5 minutes)
Have students stand with feet together. Ask them to all pick up their right foot and move their bodies slightly right, putting their right foot down. Next, pick up their left foot and move their bodies slightly left, putting their left foot down. Have the class work together and get everyone to move together 5 times in each direction.
Ask students to write a pseudocode for the actions just taken, using a for loop.
Explore
(Small Groups, 20 minutes)
Students will explore programming the Coach model to move using a for loop.
Direct students to build the Coach model using the building instructions provided 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
Ask students to examine the way the Coach model is built and brainstorm how the model can be programmed to lead a workout or dance (move back and forth) for us.
Provide students with this sample program to discuss making the Coach model move to lead us in an exercise. Ask students to explain the program and make suggestions for making the program more efficient.
Sample program:
from hub import port
import runloop
import motor
async def main():
# Run at 250 velocity for 1/4 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Prompt students as needed to change the code from repeating several lines to including a loop. Students will need to decide how many times they want to repeat the loop.
Point out to students that in this workout, the coach wants us to move one foot (motor) and then the other motor. Remind students they need to be thoughtful to name each motor something specific in order to distinguish between the two. Naming variables up front is an important step to easily reuse that information throughout the program. Discuss ideas for naming the motor variables such as using the port each motor is plugged into as seen in the sample program.
Allow time for students to modify and test their program to allow their coach to move. If students need help, they should utilize the Knowledge Base and their notes. Remind them also to watch the console for any error messages they might receive.
If needed, provide this sample code to students.
from hub import port
import runloop
import motor
async def main():
for index in range(10):
# Run at 250 velocity for 1/4 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Allow students to explore the program, changing different values, to see how the coach model moves.
Explain
(Whole Group, 5 minutes)
Discuss with students how the program worked.
Ask students questions like:
• How did you make your program more efficient?
• What does the “in range” part of the code tell the model to do?
• What happens when the program finishes the count?
Elaborate
(Small Groups, 10 minutes)
Challenge students to recognize bugs in loop programs.
Show students each program and error message. Have students discuss what needs to change in each line of code to fix the bug. Consider making the changes as a class and ensuring the program runs correctly after each change.
Debug Activity 1:
Recognize a bug in the loop code.
from hub import port
import runloop
import motor
async def main():
for index in range():
# Run at 250 velocity for 1/4 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Students should recognize that the error is in line 6. The loop function is missing a required piece, the positional argument, which is number of times you want the loop to run. Ask students to identify the number that should be included. While any number will work, students can use the comment as a hint.
Debug Activity 2:
Recognize a bug in the motor variable.
from hub import port
import runloop
import motor
async def main():
for index in range(10):
# Run at 250 velocity for 1/4 second
await motor.run_for_time(250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Students should recognize that the error is pointing to line 8 and saying that there is 1 missing argument. The motor port is not in the parenthesis.
Debug Activity 3:
Recognize a bug in the structure of the program.
from hub import port
import runloop
import motor
async def main():
for index in range(10):
# Run at 250 velocity for 1/4 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Traceback (most recent call last):
File "Project 2", line 7
SyntaxError: invalid syntax
Students should recognize that there is an error in line 8. The invalid syntax means that the program cannot recognize the command given to convert it. Prompt students to identify that we gave a for loop line of code, but then did not tell the loop what to do because we did not indent the next line of code. Every line that you want to include in the loop needs to be indented.
Students should also recognize that line 9 will need to be indented as well. Ask students to think about why no error message was given for line 9. The error is only given for the first line of code that the creates a problem. Explain to students that this can mean that you miss one error while you are fixing another.
Debug Activity 4:
Another issue that can happen is creating an error that the program does not recognize as an error.
from hub import port
import runloop
import motor
async def main():
for index in range(10):
# Run at 250 velocity for 1/4 second
await motor.run_for_time(port.B, 250, 300)
await motor.run_for_time(port.F, 250, 300)
await motor.run_for_time(port.B, 250, -300)
await motor.run_for_time(port.F, 250, -300)
runloop.run(main())
Show students the code from Activity 3 with line 8 indented, i.e. the fix to the previous error. However, what happens if we do not indent line 9. Ask students to predict the new error that will occur.
Traceback (most recent call last):
File "dance loop with coach", line 11
IndentationError: unexpected indent
Show students the error message. While students might have expected an error in line 11 for not indenting, the actual error occurred in line 12. Ask students to think about what the program was doing. Explain that the program read the loop in line 6 which connected to the action to loop in line 8 (the connection indicted by the indentation). When line 9 was not indented, the program read that the loop ended. However, when seeing an indention in line 11, the program was confused. There was nothing requiring an indention, so the program read an error.
Discuss the various error messages and ways to troubleshoot them together as a class.
Evaluate
(Group Exercise, 5 minutes)
Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What are methods to make programs more efficient?
• How can loops be used?
• What actions should you take when you receive an error message?
• Why would engineers make a robot move repetitively?
Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about creating programs with loops that are error free?
• What debugging tips did you learn today?
• 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 model using for loops.
• Debug four programs to learn tips and tricks.
• 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.