
The goal of this Python challenge was to create a program which would convert the amount of seconds given by the user (starting from 0:00), to an h:mm:ss notation. The amount of hours must not go over 23, just like on a digital clock. My solution is displayed below.
n = int(input())
sc = (n % 60) % 10 #seconds
scDec = (n % 60) // 10 #seconds as a decimal
mn = ((n // 60) % 60) % 10 #minutes
mnDec = ((n // 60) % 60) // 10 #minutes as a decimal
h = (n // 3600) % 24 #hours
print(h, ":", mnDec, mn, ":", scDec, sc, sep='') #final print
First, the program takes in the user’s input. After this, I start to calculate the seconds, minutes, and hours. Usually, in this type of problem, the first thing I would have to to would be convert to the smallest unit. This is unnecessary in this case, because the input is already in seconds.
Probably the most challenging part about this exercise is having to display the minutes and seconds in double digits. To solve this issue, I split both of those values into two variables: one for the ones, and the other for the tenths.
The final issue was making sure that the hour count stopped at 23. To do this I used the remainder from the division operation. This would mean that every time the value would equal 24, it would turn to zero.