SPIKE™ Prime with Python

Turtle Trouble

Investigate how to create and use functions in a program

45 min
Intermed.
Grades 6-8

Questions to investigate

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

Prepare

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

Engage

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

Explore

(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 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 hub import port
import runloop
import motor_pair

async def main():
   # pair motors E and F
    motor_pair.pair(motor_pair.PAIR_1, port.E, port.F)
    # move straight for 1440 degrees at a velocity of 500
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, 1440, 0, velocity=500)

runloop.run(main())

Allow 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 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:

async def frightened():  
        await motor_pair.move_for_degrees(motor_pair.PAIR_1, 1440, 0, velocity=1000)

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 hub import port
import runloop
import motor_pair

# a function to move turtle quickly
async def frightened():
    #move straight for 1440 degrees at a velocity of 1000
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, 1440, 0, velocity=1000)

# the main function
async def main():
    # pair motors E and F
    motor_pair.pair(motor_pair.PAIR_1, port.E, port.F)

    await frightened()

runloop.run(main())

Explain

(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 of 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 created a 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.

Elaborate

(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 hub import port
import runloop
import motor_pair

async def frightened():
    # move straight for 1440 degrees at a velocity of 1000
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, 1440, 0, velocity=1000)

async def normal():
    # move straight for 1440 degrees at a velocity of 250
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, 1440, 0, velocity=250)

async def struggle():
    # move straight forward for 1440 degrees at a velocity of 1000 and then repeat backwards
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, 1440, 0, velocity=1000)
    await motor_pair.move_for_degrees(motor_pair.PAIR_1, -1440, 0, velocity=1000)

async def main():
    # pair motors E and F
    motor_pair.pair(motor_pair.PAIR_1, port.E, port.F)

    await frightened()

    await struggle()

    await normal()

runloop.run(main())

Evaluate

(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?

Self-Assessment:
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 students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

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

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