Homework 4
Objectives
- Practice completing function stubs according to provided docstring comments
- Practice writing and calling functions
- Gain an understanding of parameters, arguments and return values
Note that this week’s homework will require you to read and understand function docstrings (described in section 4.7 of our textbook). You are not required to provide docstrings for the functions you create for HW 4, but HW 5 will start to evaluate these.
Exercise 4.1 Formatting Utility
In section 3.10, you learned how to format numbers in a variety of ways using f-strings. Format specifiers like f'{number:03d}'
and f'{number:f}'
can be difficult to read and understand, especially if they’re used over and over again in a program. For convenience, we can write functions that take care of these details.
As as starting point, download format_util.py
, which contains two functions: format_money
and format_time
.
Each function has a docstrings (described in section 4.7) that explains what it should do. Implement each function so that they correspond to the description in the docstring. At the end of the program, call format_money
and format_time
(twice each) to produce the following output:
300.45 CHF
7:30:00
12:08:03
Note that the reading in our textbook doesn’t cover the format specifier necessary to introduce commas to separate the thousands in large numbers. You can find more details about Python strings in the official documentation: https://docs.python.org/3/library/string.html. There is a lot there. For the purposes of this problem, just searching for “comma” in that page should get you to the information you need.
Exercise 4.2 Songs
Children’s songs and nursery rhymes often include patterns and repetition. The goal of this exercise is to write Python programs that can generate the lyrics of children’s songs without explicitly including the full text of the song in our code. Instead, we will take advantage of the ability of functions to combine a sequence of statements that can be executed over and over again.
As a starting point, download rhody.py
. When this starter code is executed, you should see the following output:
Go tell Aunt Rhody Go tell Aunt Rhody Go tell Aunt Rhody The old gray goose is dead The one she's been saving The one she's been saving The one she's been saving To make a feather bed
Finish this program so that it prints the entire song, exactly as follows:
Go tell Aunt Rhody Go tell Aunt Rhody Go tell Aunt Rhody The old gray goose is dead The one she's been saving The one she's been saving The one she's been saving To make a feather bed The goslings are mourning The goslings are mourning The goslings are mourning Because their mother's dead The old gander's weeping The old gander's weeping The old gander's weeping Because his wife is dead She died in the mill pond She died in the mill pond She died in the mill pond From standing on her head Go tell Aunt Rhody Go tell Aunt Rhody Go tell Aunt Rhody The old gray goose is dead
Once rhody.py
is working correctly, create a new program named happy.py
that generates exactly the output below. Your code should include as little repetition as follows.
If you're happy and you know it, clap your hands If you're happy and you know it, clap your hands If you're happy and you know it, and you really want to show it If you're happy and you know it, clap your hands If you're happy and you know it, slap your knees If you're happy and you know it, slap your knees If you're happy and you know it, and you really want to show it If you're happy and you know it, slap your knees If you're happy and you know it, turn around If you're happy and you know it, turn around If you're happy and you know it, and you really want to show it If you're happy and you know it, turn around If you're happy and you know it, pat your head If you're happy and you know it, pat your head If you're happy and you know it, and you really want to show it If you're happy and you know it, pat your head If you're happy and you know it, do all four If you're happy and you know it, do all four If you're happy and you know it, and you really want to show it If you're happy and you know it, do all four
When you are done, submit both rhody.py and happy.py to https://gradescope.com
Exercise 4.3 Conversions
When solving physics problems, you often need to convert from one unit to another. Create a module named convert.py
that has the following five functions. Each method must return a float value, and take a single float parameter.
celsius_to_fahrenheit
kilometers_to_miles
miles_to_kilometers
meters_per_second_to_miles_per_hour
miles_per_hour_to_meters_per_second
In contrast to the previous exercise, convert.py
should NOT call these four functions. Instead, use convert.py
as a module and create a separate file named test_conversions.py
to import convert.py
and to call the conversion functions. You will only submit convert.py
, so feel free to format the test output however you like. For example:
Note that
Exercise 4.4 Math Tricks
Write a program named math_tricks.py
that has the following two methods:
-
sphere_volume(radius)
Calculates the volume of a sphere with the provided radius. The function should return a float value.
-
normal_pdf(mu, sigma, x)
Evaluates the probability density function (at point
x
) for a normal distribution, given the mean (mu
) and standard deviation (sigma
). The function should return a float value.
Exercise 4.5 Functional Bar Chart
Our solution to HW 3.5 included some code repetition that could have been avoided through the use of functions. For this exercise you will complete the unfinished functions in bar_chart_v2.py
. The completed program will provide exactly the same functionality as your solution for HW 3.5.