Lab 1-2: Making Sense of Seconds

Lab Requirements and Specifications

In this lab, you will be creating a program that can convert a number of seconds into hours, minutes, and seconds.

The program should use the input() function to allow the user to type in a number of seconds. You can assume that the user will type in a non-negative integer. The program will perform some math, then display an output formatted like:

100 seconds is 0 hours, 1 minute, and 40 seconds.
As a reminder:
  • \(60 \textrm{sec} = 1 \textrm{min}\)
  • \(60 \textrm{min} = 1 \textrm{hr}\)

When thinking about how to calculate the number of hours, minutes, and seconds, try to think of these terms as cups - let’s start by talking about the “minute” cup.

In this example, we want to find how many full cups we can make (each full cup represents one minute). Each of these cups can only hold 60 of something, so any more and we’ll have to start filling a new cup and add one to the number of full cups we have. If this concept sounds familiar, it’s probably because it sounds like division - and that’s because it is! If we have 128 seconds, how many minutes is that? Two plus change, you probably figured, by dividing 128 by 60.

That means that 128 seconds is equivalent to 2 whole minutes, plus some seconds left over. How many seconds? What’s the remainder of 128/60? While you might be able to do it in your head, you’ll generally want to find some kind of general rule or formula that you can apply. And we learned one! Recall the modulo operator, that calculates the remainder of a quotient.

Reminder: \(a \% b\) is the remainder of \(a \div b\).

Once you’re able to convert seconds to minutes + seconds, you’ll want to repeat this thought process in order to get hours + minutes!

Please keep in mind that when input() will always give you the user’s input as a string, even if the user typed in a number! We know that 5 is not the same thing as "5".

You should name your file FILN_seconds.py, where FILN is your first initial and last name, all lowercase, no space.

Testing Your Program

You can test your program seconds.py by feeding it the following inputs and comparing your output to the result:

Seconds (input) Output
100 0hr 1m 40s
0 0hr 0m 0s
1234 0hr 20m 34s
11683 3hr 14m 43s
99999 27hr 46m 39s

The following space is provided in case you want to test code out or write it in the browser:

Taking it Further

Incorporate days, weeks, and years, into your calculations.

You will have to create your own tests for this. If you want to explore an interesting topic, visit the Unix Timestamp. Grab the long number on the left side of the page, which represents the number of seconds that have passed since January 1st, 1970. Can your program calculate how many seconds, minutes, hours, days, weeks, and years have passed since then? For the sake of simplicity you can ignore leap years.

Next Section - Lesson 02-01: Printing with String Formatting