SPIKE™ Prime with Python

# Dance Loop with Coach

Students will practice using for loops in their programs to make them more efficient

45 min
Beginner
Groep 8 tot en met de tweede klas

## 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 spike import Motor

# initialize motors and set variable names
motor_b=Motor('B')
motor_f=Motor('F')

# run motors in alternating moves to create a dance
motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor_b.run_for_seconds(0.4, -30)

motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor_b.run_for_seconds(0.4, -30)
``````

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 spike import Motor

# initialize motors and set variable names
motor_b=Motor('B')
motor_f=Motor('F')

# run motors in alternating moves 10 times
for count in range(10):
motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor_b.run_for_seconds(0.4, -30)
``````

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

Debugging Activity 1:
Recognize a bug in the loop code.

``````from spike import Motor

# initialize motors and set variable names

motor_b=Motor('B')
motor_f=Motor('F')

# run motors in alternating moves 10 times
for count in range():
motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor_b.run_for_seconds(0.4, -30)

File "lib/hub/flash/programrunner/__init__.py", line 1, in start_program
File "./projects/48509.py", line 8
TypeError: function missing 1 required positional arguments
``````

Students should recognize that the error is in line 8. 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.

Debugging Activity 2:
Recognize a bug in the motor variable.

``````from spike import Motor

# initialize motors and set variable names
motor_b=Motor('B')
motor_f=Motor('F')

# run motors in alternating moves 10 times
for count in range(10):
motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor.run_for_seconds(0.4, -30)

File "lib/hub/flash/programrunner/__init__.py", line 1, in start_program
File "./projects/7232.py", line 13
NameError: name 'motor' isn't defined
``````

Students should recognize that the error is pointing to line 13 and saying that the motor isn’t defined. The motor does not match of the two motor names that we provided. Prompt students to rename the motor to motor_b.

Debugging Activity 3:
Recognize a bug in the structure of the program.

``````from spike import Motor

# initialize motors and set variable names
motor_b=Motor('B')
motor_f=Motor('F')

# run motors in alternating moves 10 times
for count in range(10):
motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor_b.run_for_seconds(0.4, -30)

File "lib/hub/flash/programrunner/__init__.py", line 1, in start_program
File "./projects/13982.py", line 9
SyntaxError: invalid syntax
``````

Students should recognize that there is an error in line 9. 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 10 will need to be indented as well. Ask students to think about why no error message was given for line 10. 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.

Debugging Activity 4:
Another issue that can happen is creating an error that the program does not recognize as an error.

``````from spike import Motor

# initialize motors and set variable names
motor_b=Motor('B')
motor_f=Motor('F')

# run motors in alternating moves 10 times
for count in range(10):
motor_f.run_for_seconds(0.4, 30)
motor_b.run_for_seconds(0.4, 30)

motor_f.run_for_seconds(0.4, -30)
motor_b.run_for_seconds(0.4, -30)
``````

Show students the code from Activity 3 with line 9 indented, i.e. the fix to the previous error. However, what happens if we do not indent line 10. Ask students to predict the new error that will occur.

``````File "lib/hub/flash/programrunner/__init__.py", line 1, in start_program
File "./projects/5045.py", line 12
IndentationError: unexpected indent
``````

Show students the error message. While students might have expected an error in line 10 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 8 which connected to the action to loop in line 9 (the connection indicted by the indentation). When line 10 was not indented, the program read that the loop ended. However, when seeing an indention in line 12, 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.
• 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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.

## Ondersteuning voor de leraar

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.