0

I found this code on the internet and changed it a little bit, but for some reason the result is 1 second wrong;

Ex: 140153 should return 38:55:53 but is returning 38:55:52

N = int(input())
min = 60
hour = 60 * 60
day = 60 * 60 * 24

DAY = N // day
HOUR = (N - (DAY)) // hour
MINUT = (N - (DAY + (HOUR * hour))) // min
SECONDS = N - (DAY + (HOUR * hour) + (MINUT * min))

print('{}:{}:{}'.format(HOUR, MINUT, SECONDS))
  • I suggest you try your code with some different values. For example, what is the output when you enter `200000`. What should the output be? – Code-Apprentice Feb 01 '18 at 23:38

1 Answers1

1

The value of DAY is 1.

At each step, you are subtracting the number of days (1) rather than the number of seconds in each day. It doesn't affect the calculation of hours and minutes because you are doing integer division (//) (referred to as floor division in the documentation).

However, as you only want the time in hours, minutes, and seconds, you can remove DAY from your code entirely.

N = int(input())
min = 60
hour = 60 * 60
day = 60 * 60 * 24

HOUR = N // hour
MINUT = (N - (HOUR * hour)) // min
SECONDS = N - ((HOUR * hour) + (MINUT * min))

print('{}:{}:{}'.format(HOUR, MINUT, SECONDS))

N = 140153 gives you 38:55:53.

N = 200000 gives you 55:33:20.


If you just want to be able to turn seconds into hours/minutes/seconds, check this question for a more pythonic way to do it.

  • Hi Quillan, Although I still don't know why the day was subtracting 1 second from the final count, the correction you made really solved the problem, Thx a lot. – Vinicius Raphael Feb 02 '18 at 00:37