Teacher Resources: Python with Robots MS - Mission 4-3

Mission 4-3: Animatronics Lesson 3 | Python with Robots MS
Mission 4-3 Lesson Plan

Animatronics Lesson 3: Robot Moves

Students create a program with movement, random beeps, and a function that plays a note.

⏰ 45-55 min 🎯 Grades 6-8 πŸ’» CodeSpace πŸ€– CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

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

β˜‘οΈ

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
🧰

Classroom Materials

  • β–ΈCodeBot
  • β–ΈUSB cable
  • β–Έ4 AA batteries
  • β–ΈMeasuring stick
  • β–ΈOptional: Protractor
🌍

Real-World Applications

πŸ€–Functions are a form of abstraction used in real-world applications. Abstraction simplifies systems by focusing on essential features while hiding the details.
🧦You can be given a list of chores to complete without step-by-step directions for each chore.
🚘You don't need to know how an engine works to drive a car.
🎡The chorus of a song is like a function that is called multiple times.
πŸš€

Extensions & Cross-Curricular

ExtensionAdd additional movement to the robot, such as moving forward or backward, or spinning again.
ExtensionHave the 'bot return to its starting position after it spins.
ExtensionTurn on a user LED when a note plays.
MathThis program uses a function. Discuss what a function is in math. Compare and contrast.
Lang ArtsHave students write about an abstraction used in their daily lives. Supports language arts through reading instructions, guided notes, and reflection writing.
πŸ”€

Vocabulary

β–Ύ
While loop - A loop that repeats a block of code as long as a condition is True.
Random number - An integer generated using a function from the Python random module.
Function - A named chunk of code you can run anytime just by calling its name; lets you reuse code without retyping or copy/paste.
Parameter - A variable that gets its value when the function is called.
🐍

New Python Code

β–Ύ
while f < 1000: The basic structure of a while loop.
count = 0
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.
from random import randrange Import the randrange() function from the random module (library).
f = randrange(100, 1000) Generate a random integer within a given range. Possible numbers include the first value up to one less than the second value.
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.
flashLEDs()
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

2-AP-10 2-AP-11 2-AP-12 2-AP-13 2-AP-14 2-AP-16 2-AP-17 2-AP-19
πŸ“…
Preparing for the Lesson
  • 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.

πŸ’‘
Teacher Notes
  • 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

πŸ’‘Lesson Tips and Tricks
Teaching tip: You can use a variety of discussion strategies to get the most engagement from your students. For example, you can have students write their answers before asking anyone for an answer. You can use one of many think-pair-share methods. You can have students write their answer and share with someone, and then have other students share answers they heard from their peers. You can randomly select students to answer.
πŸ—£οΈWarm-up / Hook

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?
πŸ’»Mission 4 Lesson 3 Activities

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.

Teaching tip: Mission Introduction - slides 3-4. This mission is divided into four lessons. Lesson 3 completes most of the project - all except the complete fanfare. These slides review the task and goal. No questions in the mission log for the introduction slides.
Teaching tip: Objective #8 - slides 5-10. Students need to use their mission log from Mission 3 Lesson 3. They recorded data about the distance a 'bot moves at given speeds and durations, and data on spinning. Students start a new file. The program starts with a way to pause program execution until a button is pressed (slide 8), similar to Mission 4 Lesson 2. The code for moving forward and spinning is not given (slide 10). Students should use their data from Mission 3 Lesson 3. They can try different numbers, but they will work faster and with less frustration if they use their recorded data.
Teaching tip: Objective #9 - slides 11-19. Students add code near the top of their program so they can hear beeps without moving the 'bot. They do not need to press BTN-0 for the remainder of the lesson. Students learn about while loops that execute a specific number of times. A few slides discuss this type of while loop and give examples. Students practice identifying parts of a while loop in their mission log: the control variable, its initial value, the specific number used to compare, and the increment. This objective ends with a nested while loop. For deeper exploration, discuss how many times the loop will actually execute, or what the value of the control variable is when the loop stops.
Teaching tip: Objective #10 - slides 20-28. Students add code for random numbers. The random number replaces the nested while loop - students will no longer have a nested loop in their program. Slides discuss random numbers and possible values when using randrange(), and students practice with random numbers by filling in a chart in their mission log. The final step is to move the beep code from near the top to the bottom of the program. Make sure students are careful with their indenting. Review cut and paste with students if necessary.
Teaching tip: Objective #11 - slides 29-36. Students play a single note first, then the concept of a function is introduced. They take the code that plays a single note and convert it to a function. They will need to indent the current code inside the function definition and use the two parameters in the code (slide 35). When students call the function, the function call is NOT part of the function and should NOT be indented! (slide 36). The program ends without completing the full fanfare. The rest of the notes and the complete animation are programmed in Lesson 4.
Teaching tip: Extensions - slide 37. If students have time, they can do an extension. If they do an extension, they might want to do a "File-Save As" so they have their original code, which they will use in Lesson 4. No code solutions are given for the extensions.

πŸ—οΈ 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.

πŸ§‘β€πŸ€β€πŸ§‘Post-Mission Reflection

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.