Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im new to coding and i have a project on making a time converter from seconds.
Python
print('Time Converter')
print('')
print('\n This a Time Converter')


#input('Enter time in seconds:')
time = 86399#float(input('Number of seconds:'))

Hours = (time//60)//60
Minutes = Hours
Minutes %= Hours//60
Seconds %= Minutes

print('Hours:', Hours)
print('Minutes:', Hours)
print('Seconds:', Hours)


What I have tried:

I cant see any answers on the net
Posted
Updated 12-Jan-21 20:40pm
v2
Comments
[no name] 13-Jan-21 0:13am    
Are Hours, etc. floats or integers?

Not sure of your conversion algo.

Try
Python
def convert(seconds): 
    seconds = seconds % (24 * 3600) 
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
      
    return "%d:%02d:%02d" % (hour, minutes, seconds) 
      

n = 86399#int(input('Number of seconds:'))
print(convert(n))
 
Share this answer
 
Comments
CPallini 13-Jan-21 2:41am    
5.
Your code must contain Exception blocks always(Otherwise your apps doomed). Handle it
Python Exception Handling - ZeroDivisionError[^]
 
Share this answer
 
The 'zero division error' occurs because of this line
Python
Seconds %= Minutes
In order to compute the reminder, a division is performed, if Minutes is zero the troubles are going to come.

However, as pointed out by Sandeep your code is (thoroughly) incorrect, you have to look over the algorithm again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900