Lesson 13 – Dictionaries with the Finch

Required Python Concepts


Teaching Materials

Get Access

Dictionaries can be used in Python to make your code more efficient. For instance, in Lesson 10, you probably used a series of elif statements to choose the buzzer frequency that corresponded to a particular note. It may have looked something like this:

You could use the dictionary shown below to make this much easier.

Exercise 1:

Modify your playNote() function from Lesson 10 to use the dictionary shown above. How does this function compare to your original one?

The elements of a dictionary can also be lists. For instance, you can create a dictionary that relates color names to lists containing the corresponding red, green, and blue values. Then you can use the color name key to set the color of the Finch’s beak.

Exercise 2:

Add your own colors to the color dictionary shown above. Use this dictionary to write a program that lets the user enter a color name and then turns the Finch beak that color. Your program should check that the user has entered a valid color name and give them an error if needed.

A dictionary can even contain the name of a function! For example, each list in this dictionary contains a Finch method name and some parameters for that method.

Exercise 3:

Using the above dictionary, what will the Finch do when you run this line of code? Make a hypothesis, and then test it.

Exercise 4:

Expand the dictionary above to link other characters to different movements of the Finch. Then use this dictionary to write a program that lets the user drive the Finch using the keyboard.