New Required Python Skills: for loops, string manipulation
Resources available for teachers. Get Access
In this lesson, you will teach your Finch to sing! Your Finch will play a song represented by a string. The string will be composed of notes – acceptable characters are A, a, B, b, C, c, D, d, E, e, F, f, G, and g. An example song might be “CCCdEedefG.” You will use the Finch’s buzzer to generate the correct musical note for each letter. Uppercase characters represent long notes, and lowercase letters are short notes.
You will be working on a single program for this entire lesson, but it is broken down into exercises so that you can work on one step at a time.
Start your program by defining a string named validNotes that contains all the acceptable characters. Then ask the user to enter a song as a string.
- Be sure to give the user instructions so that they understand what input is acceptable.
- Next, use a for loop to check each character of the user’s string to see if it is in validNotes. If any character in the user’s string is not in validNotes, then the user’s song is not valid.
- If the song is valid, tell the user that the Finch will now play it. Otherwise, tell the user that the song is not valid.
Write a function to play a single note. This function should take a single character as input. Since you have already checked your user input, you may assume that the character is valid.
- If the letter is uppercase, the length of the note should be 1.2 seconds. If the letter is lowercase, the length of the note should be 0.6 seconds.
- Determine the frequency for the letter. Use the following mapping of characters to frequencies:
- Aa – 880
- Bb – 988
- Cc – 523
- Dd – 587
- Ee – 659
- Ff – 698
- Gg – 784
- Once you have determined the duration and the frequency of the note, play it with the Finch’s buzzer.
Use your function to play each of the notes in the song. Remember, you should only play a song if it is valid! Here are some songs that you can use to test your program:
When a note is played, you should also light up the Finch’s beak; each frequency should have a unique color, though notes with the same frequency but different durations (like ‘A’ and ‘a’) can have the same color.