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

I want to get the output in the form of HH:MM:SS, so for example get 5 seconds to be expressed as 05 seconds? Python

Ive written this code and ive obtained the desired output however it is not in the correct format. Essentially the codewars challenge was to take a number of seconds up to around 350000 and then split it into hours then minutes then seconds. For example my code would take x seconds then express it as y:z:p (where y, z and p represent single digit integers) however i would like my code to express it as 0y:0z:0p unless y,z or p are already two digit integers.

Here is my code:

secs = 350000
mins = secs/60
hrs = mins/60 
hrs_hol = int(hrs)
print(hrs_hol, hrs)
hrs_rem = hrs-hrs_hol
print(hrs_rem)
mins_from_hrs_rem = hrs_rem*60
mins_hol = int(mins_from_hrs_rem)
mins_rem = mins_from_hrs_rem - mins_hol
secs_from_min_rem = mins_rem*60
secs_final = int(secs_from_min_rem)
H = str(hrs_hol)
M = str(mins_hol)
S = str(secs_final)
print(H+':'+M+':'+S)

Thankyou!

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 :

Remove the assignments to H, M & S then:

print(f'{hrs_hol:02d}:{mins_hol:02d}:{secs_final:02d}')

Here we use an f-string and a format specifier that indicates a minimum of two digits (left-padded with zero if necessary)

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