Site Search

Python: Lesson 9 – Functions with the Finch

Resources

In the previous lessons, you have learned to use the existing Finch methods to write your programs. In this lesson, you will write your own functions to make the Finch do new things. For example, you can write a function to make the Finch turn 90°.

Exercise 1:

Try out the function above for your robot. Adjust the wait time to make your robot turn 90°. Notice that the Finch is moving at a low speed so that it will make an accurate turn.

You can also create Finch functions that take parameters. So far in this course, you have adjusted wait time and speed to change how far the Finch moves, but moving a specific distance has required some trial and error. Now you can write a function that accepts a distance in centimeters and makes the Finch move that distance! To do this, you will measure how far the Finch moves for different time periods, and then you will use this information to figure out how to calculate the time needed to move a particular distance.

Exercise 2:

For this activity, you will need tape and a measuring device, such as a yardstick or a tape measure. Write a program to make the Finch move forward with a wheel speed of 0.5 for 0.5 seconds. Then measure how far the Finch moves. You may find it helpful to mark the Finch’s starting point with tape.

Record the measured distance under “Trial 1” in the data table below. Repeat this process twice to record two more trials for a time of 0.5 seconds. Continue to change the movement time and measure distance to complete the data table.

Use your data to write an equation for distance in terms of speed and time. Based on your data, what does a Finch speed of 0.5 correspond to in cm/s?

Solve your equation for time. Now you can take a distance as input and calculate the time required to move that distance.

Exercise 3:

Use your equation from Exercise 2 to write a function that takes a distance in centimeters as a parameter. The Finch should move that distance at a speed of 0.5.

Exercise 4:

Use the two Finch functions you have written in this lesson to create a third function named drawSquare(). This function should accept a distance in centimeters and have the Finch move in a square with a side length equal to the input distance.

You can also write Finch functions that return a value! For example, the function below returns the larger of the two light values measured by the Finch.

Exercise 5:

Write a function that returns the temperature measured by the Finch in Fahrenheit.