SPIKE™ Prime with Python

Turtle Trouble

Investigate how to create and use functions in a program

45 minutos
6.º de primaria a 2.º de secundaria

Questions to investigate

• How can lines of code be used over and over again?


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


(Group Discussion, 5 minutes)

Engage students in a discussion about how we impact the world around us.

Guide the discussion by showing a video of a sea turtle trapped in plastic bags or other trash items found in the ocean. Continue the discussion on other ways humans have an impact on the environment.


(Small Groups, 20 minutes)

Students will investigate how an animal reacts to pollution to their habitat.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Hopper 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. In this lesson, the hopper will represent a sea turtle.

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.  

Get Moving
Prompt students to think about how they can program the sea turtle to move its flippers in order to swim or walk on land. Remember, turtles move slowly.

Ask students to write a program that will make the turtle’s flippers move.

Sample Program:

from spike import PrimeHub, MotorPair

hub = PrimeHub() 

flippers = MotorPair('E','F')

flippers.move(100, 'cm', 0, 25)

All students time to program their turtles.

Get Moving through Trash
Discuss how turtles move. Turtles do not swim or walk at one speed. When a turtle is frightened, it will swim faster. When it is struggling to get out of a plastic bag or other trash found in the ocean, the turtle might move its flippers back and forth.

Ask students to modify their program to allow different reactions or lines of code to run based on the situation. One way to do this is through using a function. A function is a block of reusable code that can be called at any time in the program.

Explain to students in order to set up, or define, a function start with a def line of code followed by a name for the function, parenthesis, and end with a colon. (Remind students to name the function something that makes sense to the action in the function.)

For example:

def frightened():
flippers.move(100, 'cm', 0, 100)

Explain the sample program to students to see how the function is defined and then called in the program to be used.

Sample program:

from spike import PrimeHub, MotorPair

hub = PrimeHub() 
flippers = MotorPair('E','F')

\#defining the function
def frightened():
flippers.move(100, 'cm', 0, 100)

while True:
\#calling the function


(Whole Group, 5 minutes)

Discuss with students how the program worked.

Ask students questions like:
• Explain your function and how functions work?
• What is the benefit to using a function in your program?
• What is the difference between defining a function and calling a function?

Functions are used when a block of code needs to be repeated multiple times in a program. In our program, we one created on function and called it one time. We can call this function as many times as we wish throughout the program. By creating a function, we shorten the program by using lines of code over and over again.


(Small Groups, 10 minutes)

Challenge students to add two more functions to program the turtle to swim/walk at a normal speed and to struggle when getting trapped in a plastic bag.

Ask students to call each function when needed in the program.

Sample code:

from spike import PrimeHub, MotorPair
from spike.control import wait_for_seconds

hub = PrimeHub() 
flippers = MotorPair('E','F')

\#defining the three functions
def frightened():
flippers.move(100, 'cm', 0, 100)

def normal():
flippers.move(100, 'cm', 0, 25)

def struggle():
flippers.move(100, 'cm', 0, 100)
flippers.move(100, 'cm', 0, -100)

while True:
\#calling the functions 




(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when you called the functions?
• Does it matter which order the functions are defined? Why or why not?
• What are the benefits of using functions?

Have students answer the following in their journals:
• What did you learn today about using functions?
• 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.

Soporte para el profesor

Students will:
• Write a program that will make the turtle’s flippers move
• Modify a program to allow different reactions or lines of code to run based on the situation

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

Legal NoticePrivacy PolicyCookies

LEGO, the LEGO logo, the Minifigure, DUPLO, the SPIKE logo, MINDSTORMS and the MINDSTORMS logo are trademarks and/or copyrights of the LEGO Group. ©2024 The LEGO Group. All rights reserved. Use of this site signifies your agreement to the terms of use.