Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Evaluating time with python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading