In this problem, you will implement five simple functions that we’ll use to help us process times (or durations). The first three functions will help us convert a raw duration given in seconds to a number of hours, minutes, and seconds. For example, 7326 seconds corresponds to 2 hours, 2 minutes and 6 seconds. This is because each hour is 3600 seconds, so 2 hours accounts for 7200 seconds; the remaining 126 seconds constitutes 2 minutes (at 60 seconds each) and 6 seconds. Today’s program will take a single command line argument giving a number of seconds as a command line argument, then print the number of hours, minutes, and seconds in hh:mm:ss format.
To do this, you’ll implement five functions that break down this slightly complicated task into smaller, less complex pieces:
get_hours(seconds)
returns the number of whole hours in
a given number of seconds.get_minutes(seconds)
returns the number of whole
minutes in the given number of seconds. The number of minutes does not
count those accounted for by hours, meaning, for example, that
get_minutes(7326)
returns 2, not 122.get_seconds(seconds)
returns the number of seconds left
over after hours and minutes are accounted for.padprint(number)
prints out the given number, but
inserts a zero before one-digit numbers so that the printed number has
at least two digits.print_hms(seconds)
prints the duration represented by
the given number of seconds in hh:mm:ss format. For example,
print_hms(7326)
prints 02:02:06
.Finally, implement the main program behavior described above inside a main guard.
Pay particular attention to the difference between returning and printing. The first three functions return their results, while the last two print some output and do not return anything. A run of the main program (which has been written for you, but relies on the functions you’ll implement) looks like this:
>>> %Run P10_hms.py 7425
02:03:45
Write a function parrot
that takes one string
argument (s
) and one integer argument (n
) and
prints that string n
times with a space in between; at the
end, the function should print a single newline. Examples:
>>> parrot("hello!", 3)
hello! hello! hello!
>>> parrot("click", 5)
click click click click click
>>>
Write a function rectangle_area
that takes two
numbers height
and width
and
returns the area of a
height
-by-width
rectangle. Recall that a \(h \times w\) rectangle has area \(h*w\).
Write a function circle_area
that takes one number
radius
and returns the area of a circle
with that radius. Recall that a circle with radius \(r\) has area \(\pi r^2\).
Write a function get_float
that takes a string,
prints that string, then prompts a user for a floating-point number and
returns their input converted to a float.
>>> get_float("Enter a radius: ")
Enter a radius: 4
4.0
(Note: in the above, 4
is entered by the user, and
4.0
is printed on the Thonny shell in dark blue, showing
the return value of the call to get_float
)
Use the functions from the prior three problems to create a geometry game as follows. The program takes two command line arguments specifying the width and height of a rectangle. Then, it repeatedly prompts the user for a radius. For each radius provided, print a message saying whether the circle with the given radius has area greater than, smaller than, or equal to the rectangle’s. If the user enters a negative number, the program should terminate.
Write a function print_banner
that takes a string
argument and prints that string, surrounded by a rectangle of
#
symbols. Here are some examples of how the function might
be called:
>>> print_banner("Hello!")
########
#Hello!#
########
>>> print_banner("You enter a room with two doors on the opposite wall.")
#######################################################
#You enter a room with two doors on the opposite wall.#
#######################################################
>>>
Assume there are no newline characters (\n
) in the
string you’re given, and that the string can be printed on one
line.