I need to write a program that reads in seconds as input, and outputs the time in hours, minutes, and seconds using python.
seconds = int(input())
minutes = seconds // 60
hours = minutes // 3600
seconds_left = + (seconds - hours)
print(f'Hours: {hours}')
print(f'Minutes: {minutes}')
print(f'Seconds: {seconds_left}')
This is what I’m currently running and it’s not getting the desired output. Question in mind uses 4000 as an input and outputs 1 hour, 6 min, and 40 seconds
>Solution :
When you divide to get (e.g.) the hours, you should also take the mod in order to just carry forward the remainder:
>>> seconds = 4000
>>> hours = seconds // 3600
>>> seconds = seconds % 3600
>>> minutes = seconds // 60
>>> seconds = seconds % 60
>>> hours, minutes, seconds
(1, 6, 40)
This is equivalent to multiplying the int quotient by the divisor and subtracting:
>>> seconds = 4000
>>> hours = seconds // 3600
>>> seconds -= hours * 3600
>>> minutes = seconds // 60
>>> seconds -= minutes * 60
>>> hours, minutes, seconds
(1, 6, 40)