Teacher Resources: Python with Robots MS - Mission 4-3
Animatronics Lesson 3: Robot Moves
Students create a program with movement, random beeps, and a function that plays a note.
Overview
Mission 4-3 builds on students' earlier work with movement and sound. Students use data from Mission 3 Lesson 3 to move CodeBot forward a specific distance and spin it in a full circle. They then explore while loops that execute a specific number of times, generate random integers using Python's random module, and write their first function with parameters to play a musical note.
π― Mission Goal: Students will write a RobotMoves program that combines movement, a while loop, random beeps, and a user-defined function that plays a note.
Learning Targets
- I can move the 'bot forward a specific distance.
- I can spin the 'bot in a full circle.
- I can import the random module.
- I can generate a random integer between a start and stop value.
- I can use the random integer in a program.
- I can use a while loop that executes a specific number of times.
- I can nest a while loop inside another while loop.
- I can define and call a function with parameters.
Key Concepts
-
Python's random library makes it easy to work with random numbers. This program uses the
randrange()function. -
The
randrange()function has two parameters: start and stop. The lowest possible result is the start number; the highest is one less than the stop number. - A function is a named chunk of code you can run anytime by calling its name. A function can be called multiple times in a single program.
-
A function definition and function call always include
().
Assessment Opportunities
- Mission 4 Lesson 3 Log (digital)
- Submit completed program RobotMoves
- Submit the program with extensions
- Mission 4 Obj. 8-12 Review Kahoot!
Success Criteria
- Move the 'bot forward a specified distance
- Spin the 'bot in a complete circle
- Use a while loop that executes a specific number of times
- Generate a random integer
- Use the random integer as an argument for pitch
- Define a function for playing a note
- Call the function for playing a note
Digital Resources
Classroom Materials
- βΈCodeBot
- βΈUSB cable
- βΈ4 AA batteries
- βΈMeasuring stick
- βΈOptional: Protractor
Real-World Applications
Extensions & Cross-Curricular
π€
Vocabulary
βΎ
π
New Python Code
βΎ
while count < 10:
Β Β Β Β count = count + 1 A while loop starts a block of code - all commands to be repeated must be indented inside the loop. A loop has a control variable, which must be initialized before the loop starts and incremented inside the loop.
randrange() function from the random module (library).
def flashLEDs():
{indented block of code}
def note(freq, duration):
spkr.pitch(freq)
sleep(duration)
spkr.off()
sleep(0.05)
Define a function. A function definition always uses (), even if there are no parameters. All code to be included in the function is indented inside the function definition.
note(349, 0.4) Function call. A function call uses the name and
(), but not the def. If the function has parameters, the call includes arguments - values that get passed to the parameters.
π
Standards
βΎ
CSTA
- Have the assignment from Mission 3 Lesson 3 available for students to use.
- Decide what materials you want to use for presenting the lesson. The slides can be projected on a large screen.
- Be familiar with the mission log assignment and the questions students will answer. Prepare the assignment to give through your LMS.
- Look over the example code for Objectives 9 and 10.
- If you have a word wall or another form of vocabulary presentation, prepare the new terms.
- Use the slides instead of instructions in CodeSpace and CodeTrek. All goals will still be met, but students are asked to create a new program and only focus on the new material. The programs will be combined in Lesson 4. If you choose to use the instructions in CodeSpace and the CodeTrek, then don't use the slides.
- Students should refer to their data from Mission 3 Lesson 3 to help with moving forward and turning.
- Start a new program for this lesson. The code will look shorter and slightly different than in CodeTrek, but all goals will be met.
Lesson Outline
Slide 2 - Students can write in their log first and then share, or discuss first and then write in their log. These warm-up questions review code from Mission 4 Lesson 1. Students can share their answers, or compare with each other.
- Question: What lines of code play a beep?
- Question: What lines of code debounce a button?
The Chrome browser works best, but other browsers also support CodeSpace. Each student will complete a Mission Log. Students could work in pairs through the lesson, or they can work individually.
ποΈ Optional: Mission 4 Obj 8-11 Kahoot! Review. A review Kahoot! is available for these four objectives. You can do the Kahoot together as a class, or assign it independently.
The post-mission reflection asks students to review the code used in the program. You can use an extension or cross-curricular activity as a post-mission activity.
Functions are a form of abstraction. Go over the real-world applications in the lesson plan. Have students come up with their own examples - you can even have students act out functions and function calls.
End by collecting the Mission 4 Lesson 3 Log.