Course 5.2Lesson 6 of 6
In this project we build a toolbox of small functions that each do one job: the area of a rectangle and of a circle, a temperature conversion, a leap year test and a letter grade. Each function takes its values as parameters and gives its answer with return, has a docstring, and is tested with a table of cases. The main program is then a few short lines that use the tools. We plan the tools first, build and test them one by one, and extend the toolbox with tools of our own.
Try the code and the simulations beside the lesson.
After this lesson you can
return and a docstring,You use the same little calculations again and again: the area of a room, the area of a circle, a temperature in degrees Celsius changed into Fahrenheit, whether a year is a leap year, a letter for a mark. Instead of writing them again in each program, you put them in functions, in a toolbox that you can use in every program.
Write each tool as a sentence: its name, what comes in, what comes out.
| Tool | In | Out |
|---|---|---|
area_rectangle | length, width | the area |
area_circle | radius | the area, using 3.14159 for pi |
celsius_to_fahrenheit | degrees C | degrees F |
is_leap_year | a year | True or False |
grade | a mark | a letter |
Also write one expected answer for each, by hand, from what you know: a 6 by 4 room has an area of 24. A circle of radius 3 has an area of about 28.27. 37 degrees Celsius is 98.6 Fahrenheit. 2024 is a leap year, and 1900 is not. A mark of 82 is a B.
Use the idea of the last lesson. For each tool, write a table of cases, and run a loop. For area_rectangle: (6, 4) gives 24, and also a zero side and a decimal side. For is_leap_year: 1900 (no), 2000 (yes), 2023 (no), 2024 (yes), 2100 (no). For celsius_to_fahrenheit: 0 gives 32, 100 gives 212, and -40 gives -40. That last one is a famous case: at -40, both scales show the same number.
Each tool does one job and returns its answer. The program combines them, like the buttons of a calculator.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
fahrenheit_to_celsius. The reverse tool. A good test: converting there and back gives the start value.area_triangle. Half of base times height.bmi. A weight in kg and a height in metres give a body mass index.is_prime. A number is prime when only 1 and itself divide it. Use a loop inside the function.average. A list of numbers in, the average out. What should it do for an empty list?Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
is_prime to your toolbox, test it with the numbers 1 to 20, and write down which numbers are prime.return out, and has a docstring.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
celsius_to_fahrenheit(100)?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.