At My Fingertips

Documentation

Countdown Timer

Abstraction

Have a look at these geometric figures:

ABC
10355

We could call them stopwatches or timers or something like that. Here are some similarities:

  • They all have a circular outline
  • They all have 12 tickmarks
  • They all have a triangular crown at the top
  • They all show a large number in the center
  • The all show the word "min" below the number
  • They all show a faintly colored circular sector

Our goal is to develop a function named countdown_timer that can produce any of the above figures:

def countdown_timer(...) -> Graphic:

Now let's focus on the differences between the above graphics, so we can figure out what parameters our countdown_timer function should have.

We see two differences:

  • the large number
  • the angle of the circular sector

Are those two differences independent, or is there a relationship between the number and the angle?

There is a direct relationship: the angle represents the number of minutes, like in an analog clock. Thus, we only need one parameter. For example, we can provide the number of minutes, and the function can compute the angle from that automatically.

def countdown_timer(minutes: int) -> Graphic:

Implement

Let's start by defining some constants that capture the style (colors and font) of our graphic, as well as its size.

Loading...

Before starting to "hack", you may want to decompose the graphic into serveral components, like:

  • the ticks
  • the ring
  • the crown
  • the number
  • the minute label
  • the sector

You could create a separate function for each component, and test that function on its own. After each of those functions work, implement the countdown_timer function to compose these components into the overall graphic.

Loading...

Play With Parameters

Play with your countdown_timer function, creating timers that show a different number of minutes.

What happens if you provide 60 as an argument? What about numbers greater than 60? What about less than 0?

You may want to add an assert statement at the top of your minute_timer function to reject argument values outside the range your function supports.

Loading...

Save in Toolbox

Save your countdown_timer function in your toolbox. It might come in handy in the future, for example if you ever need a set of timer icons, for different times, to include in a presentation you create.

icons

You are in control! You don't need to use the exact design given above. You can tweak your code so it produces countdown timers in a style you like!


This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Countdown Timer

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version 19a6bb6 (Mon, 27 Jan 2025 12:33:26 GMT)