VR - Mission 14: Music Box

Mission 14 Lesson Plan

Music Box

Students turn CodeBot into a jukebox, using a dictionary of notes and Python's file operations to open, read, write, and play songs stored in text files.

⏰ 2-3 hours 🎯 Grades 9-12+ πŸ’» CodeSpace πŸ€– Virtual CodeBot 🐍 Python
View Lesson Outline
πŸ“‹

Overview

In this mission, students turn CodeBot into a jukebox. They build a dictionary of notes and pitches, then use a list of notes to play a song, and expand that to a matrix of notes and beats. Along the way they learn Python's file operations: opening, reading, writing, and closing text files, and converting string data pulled from a file into a matrix (a list of lists) the 'bot can play. The mission wraps up with a jukebox program that cycles through songs when a button is pressed.

🎯 Mission Goal: Students will turn the CodeBot into a jukebox and learn about Python's file operations.

🎯

Learning Targets

  • I can use a dictionary of notes to play different pitches.
  • I can use a list of notes to play a song by accessing the dictionary of notes.
  • I can open, read, write to, and close a text file.
  • I can play a tune using a matrix of notes and beats.
  • I can convert string data from a file into a matrix.
πŸ’‘

Key Concepts

  • A dictionary of notes is useful for storing data that is used frequently.
  • The Python file system allows you to open, read, and write to a file in many ways.
  • You should always close or flush a file after it is opened.
  • String data can be converted to an integer.
  • String data from a file can be converted to a list of lists using file operations.
βœ…

Assessment Opportunities

  • Submit program code, or give students printed code and have them explain each line: Objective 3, Objective 6, Objective 8
  • Give extra practice with file operations
  • Give extra practice with iterating over a list
  • Code Tracing Chart as a debugging tool
  • Submit final jukebox program
  • Mission 14 Review Kahoot!
β˜‘οΈ

Success Criteria

  • Create a dictionary of notes and pitches.
  • Iterate over a list of notes and beats to play a song.
  • Open, read, and close a file.
  • Convert string data from a file into a matrix (list of lists).
  • Program CodeBot to cycle through songs like a jukebox when a button is pressed.
🧰

Classroom Materials

  • β–ΈComputer or Chromebook with internet access
🌍

Real-World Applications

πŸ“‚Programmers write code all the time that pulls in files they or someone else created. Data comes from many sources, and collecting it into a file for sharing and use in other applications is a common real-world task.
πŸ’¬Discuss where data files come from and how and where they get used, from music playlists to sensor logs to shared spreadsheets.
πŸš€

Extensions & Cross-Curricular

ExtensionUse a button press to scroll forward, and the other button to scroll backward.
ExtensionAdd a light show by using LEDs while a song is played. You can even have a different light show for each song.
ExtensionAdd CodeBot dances to the songs.
ExtensionUse a button to speed up the beat duration and make songs play faster.
Lang ArtsHave students explain how lists and dictionaries help manage the complexity of the program and how their program would be different without them.
Performing ArtsHave students share music from their culture or heritage.
MathUse the pitch-per-note chart from Objective 1 to graph the frequency of each note, then extrapolate information for other notes.
ScienceHave a lesson on sound waves.
πŸ”€

Vocabulary

β–Ύ
Scientific Pitch Notation:Writing musical notes in the form C5. The letter is the note, and the number is the octave. C4 is middle C on the piano.
split() function:Turns a string into a list.
File operations:Reading, writing, and using files in a Python program.
Mode:The way a file is accessed, such as read-only, read and write, or write-only.
Flush:Guarantees that any buffered data is saved to the filesystem (close or flush function).
Matrix:A multidimensional list (lists inside a list).
int() function:Takes a value (string or float) and converts it to an integer.
🐍

New Python Code

β–Ύ
notes = ['C', 'G', 'A', 'E', 'F'] for note in notes: f = freqs[note] spkr.pitch(f) sleep(beat_duration) Iterate over a list of strings.
text = 'E E G C D E F A' notes = text.split() Split a string into a list.
f = open('song_name', 'r') Open a file - read-only.
text = f.read() Read the data from a file.
f.close() Close a file.
f = open('new_song', 'w') Open a new file to write to.
f.flush() Flush a file (all data is saved before closing).
song = [ ['E', 1], ['D', 1], ['C', 2] ] Create a matrix of notes and beats (list of lists).
for note, beats in song: f = freqs[note] spkr.pitch(f) sleep(beats * beats_duration) Unpack a matrix using a for loop.
beats = int(str_beats) Convert a string to an integer.
f = open('another_song.csv', 'r') file_lines = f.readlines() Get a list of strings, one from each line in a file.
song = [] for line in file_lines: note_beat = line.split(',') song.append(note_beat) Split a list of strings into a list of lists (comma delineator).
πŸ“

Standards

β–Ύ

CSTA Standards - Grades 9-10

3A-CS-03 3A-DA-10 3A-AP-13 3A-AP-14 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21 3A-AP-23 3A-AP-26

CSTA Standards - Grades 11-12

3B-CS-02 3B-DA-05 3B-AP-10 3B-AP-12 3B-AP-14 3B-AP-15 3B-AP-16 3B-AP-22 3B-AP-23

Certiport IT Specialist: Python Standards

1.1 1.2 1.3 1.4 2.1 2.2 3.1 4.1 4.2 6.1

PCEP: Certified Entry-Level Python Programmer

1.1 1.2 1.3 1.4 1.5 2.1 2.2 3.1 3.2 3.4 4.1 4.2
πŸ“
Preparing for the Lesson
  • Remember to zoom in enough inside the 3D environment to hear the speaker.
  • Preview the objective walkthrough videos (Obj 2, 5, and 7) so you can point students to the right one when they get stuck.
  • Have the Code Tracing Chart ready to use as a debugging tool for Objectives 3, 6, and 8.
  • Plan checkpoints for the code submissions at Objective 3, Objective 6, and Objective 8, plus the final jukebox program.

πŸ§‘β€πŸ«
Teacher Notes
  • Remember to zoom in enough inside the 3D environment to hear the speaker.
  • When typing a long list, it can be divided up into more than one line, just like a dictionary, for easy reading.
  • Make sure students include the file extension in the name for the open command.
  • Programming concepts used are: dictionaries, iterating over a list, using a matrix, and file operations. Review them as needed.
πŸ—ΊοΈ

Lesson Outline

πŸ—£οΈWarm-up / Hook

Ask students how a jukebox or music app "knows" which song to play and how it stores the notes of a song. Introduce the idea that CodeBot can store songs the same way, as data in a dictionary, a list, and eventually a file, and that today's mission builds toward a working jukebox program.

1️⃣Objectives 1-3: Notes, Pitches, and Playing a Song
  • Build a dictionary of notes and pitches using Scientific Pitch Notation, then iterate over a list of notes to play a song by looking up each pitch in the dictionary.
  • Reference Mission 14 Obj 2 as needed.
  • Submit program code, or give students printed code, and have them explain each line for Objective 3.
Teaching tip: When typing a long list of notes, let students divide it across multiple lines, just like a dictionary, for easier reading.
2️⃣Objectives 4-6: File Operations
  • Open, read, and close a text file. Practice the different file modes (read-only, write, and read and write), and always remember to close or flush a file after opening it.
  • Reference Mission 14 Obj 5 as needed.
  • Give extra practice with file operations, then submit program code or have students explain printed code for Objective 6.
Teaching tip: Make sure students include the file extension in the name they pass to the open command.
3️⃣Objectives 7-8: Matrix of Notes and Beats
  • Build a matrix (a list of lists) of notes and beats, then unpack it with a for loop to play a tune with both pitch and rhythm.
  • Reference Mission 14 Obj 7 as needed.
  • Give extra practice iterating over a list, then submit program code or have students explain printed code for Objective 8. Use the Code Tracing Chart as a debugging tool if students get stuck.
Teaching tip: Review int(), split(), and how string data from a file becomes a matrix - this is where most students need extra support.
4️⃣Objectives 9-10: Building the Jukebox
  • Convert string data pulled from a file into a matrix of notes and beats, then program CodeBot to cycle through songs like a jukebox when a button is pressed.
  • Have students submit their final jukebox program.
πŸŽ‰Wrap-up / Review

Close with the Level-1 Mission 14 Review Kahoot! to reinforce dictionaries, lists, matrices, and file operations before moving on to the Unit 6 Remix & Assessment.