SPIKE™ Prime with Python

Break Dancer Break Down

Investigate strategies for debugging programs

45 min
Intermed.
Years 7-9 or Key Stage 3

Questions to investigate

• How do software engineers identify and fix bugs in a program?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Discuss with students how to identify if a problem is with the hardware (model) or the program.

In the Testing Prototypes lesson, students identified ways to test and iterate on their model in order to find a final solution. However, there was no programming element involved in their bridge building. Now students will need to find ways to identify if problems are in the model design or the program.

Watch the video of the break dancer model dancing to get an idea of how the model should move.
PLACEHOLDER FOR VIDEO

Discuss with students the different ways the dancer can move. Ask students questions like:
• Which parts of the model were moving?
• How can you replicate that movement with the model?
• If something goes wrong, how will you know if it is the model or the program?

Explore

(Small Groups, 20 minutes)

Students will build a break dancer model to investigate identifying bugs and fixing them.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Break Dancer model. Ask students to build the model. The building instructions are also available 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.

Test the Model
Students will identify that there are bugs in the program when trying the sample code.

Ask students to examine their model closely to identify how it should move. Students should try moving the motors and other parts of the model to see if it will move like the video based on the discussion in the engage section.

Allow students time to investigate how the model moves without creating a program. Ask students questions like:
• Did the model move in similar ways to the video?
• Does anything on the model seem to not be working properly?

Test the Program
Ask students to type this program into the programming canvas. Ask students to run the program. 
from spike import ColorSensor, Motor
from spike.control import wait_for_seconds

from hub import port
import runloop
import motor

async def main():
    # Move legs motor to 0 degrees
    await motor.run_to_absolute_position(port.F, 0, 600)
    # Move arms motor to 0 degrees  
    await motor.run_to_absolute_position(port.D, 0, 400)
    # Repeat arm and leg movement 10 times
    for x in range(10)
        await motor.run(port.F, 600)
        await motor.run(port.D, 400)

runloop.run(main())

Allow students to run the program. Notice an error message is received.

Discuss with students that the error message shows there is a bug in the program. Students will need to check the lines of code carefully to find and fix the bug. This process is known as debugging a program.

Students should identify that the first way to identify if something is wrong with a program that an error message will be received. Look at the error message together:

Traceback (most recent call last):
File "Break Dancer", line 13
SyntaxError: invalid syntax

The error points students to the line to check for the message. Allow students time to try to troubleshoot this error.
Note: a colon: is needed at the end of the line to read the code associated with the for loop.

Explain

(Whole Group, 5 minutes)

Discuss the program with students and why it does not seem to be working correctly.

Ask students questions like:
• What does it mean to debug a program?
• How is the error message helpful?
• Why was it important to test the model before running the program?
• What does “syntax error” mean?

Discuss the types of errors with students:
Syntax errors are grammar errors in our code. The code is written in a way that the machine can understand it. Typos and misuse of punctuation or parenthesis are good examples of reasons you will get a syntax error.
Logic errors are found when running a program and are the result of not receiving the expected output. The code will run, but not produce the expected result or run as expected.
Runtime errors occur while the program is running. The program often will run fine for some time until it encounters the problem and then end in an error.

Elaborate

(Small Groups, 10 minutes)

Allow students additional time to explore the types of errors.

Explain to students that normally they would try hard not to have bugs in their program. However, the next challenge will be for them to try to find different bugs that can occur. Challenge students to change their program from the explore phase to see what types of errors they might find. See if they can replicate all three types.

Example Errors:

Example 1:

Change the line await motor.run_to_absolute_position(port.F, 0, 600)
To read await motor.run_to_absolute_position(port.A, 0, 600)   

Students will receive a runtime error message. The program does not notice there is a problem until it starts running. Then it realizes that there is not a motor plugged into port A.

Traceback (most recent call last):
File "Break Dancer", line 16, in <module>
File "Break Dancer", line 8, in main
OSError: [Errno 19] ENODEV

Example 2:

Change the line for x in range(10):
To read for x in range():

Students will receive a Type Error message, which is a type of syntax error, that indicates there is a missing function. The for loop requires a number to indicate how many times it should loop.

Traceback (most recent call last):
File "Break Dancer", line 16, in <module>
File "Break Dancer", line 12, in main
TypeError: function missing 1 required positional arguments

Note: There are ways to change the numbers to get an unexpected result in this program. However, the console will not show it as a logic error because the program has not specified an intended outcome against which to measure it.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.  Ask students questions like:  
• How does testing help identify bugs in your program?
• How can you tell the need to troubleshoot hardware and not the program?
• What are the types of errors you can receive? How you can use them to fix your program?

Self-Assessment
Have students answer the following in their journals: 
• What did you learn today about identifying and fixing bugs?
• 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. 

Teacher Support

Students will:
• Identify a problem and debug the program.

• SPIKE Prime Set
• Device with SPIKE App installed
• Student journal

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