def convert_seconds(ms):
float_num = False
if type(ms) == float:
float_nums = ms - int(ms)
ms = int(ms)
float_num = True
hour = ms / 3600
minute = (ms - hour * 3600) / 60
if float_num:
second = ms - (hour * 3600 + minute * 60) + float_nums
else:
second = ms - (hour * 3600 + minute * 60)
format_hour = "hour"
format_minute = "minute"
format_second = "second"
if hour > 1 or hour == 0:
format_hour = "hour" + "s"
if minute > 1 or minute == 0:
format_minute = "minute" + "s"
if second > 1 or second == 0:
format_second = "second" + "s"
result = (str(hour) + " " + format_hour, str(minute) + " " + format_minute, str(second) + " " + format_second)
return result[0] + ", " + result[1] + ", " + result[2]
print convert_seconds(3600)
#>>> 1 hour, 1 minute, 1 second
print convert_seconds(7325)
#>>> 2 hours, 2 minutes, 5 seconds
print convert_seconds(7261.7)
#>>> 2 hours, 1 minute, 1.7 seconds
S'il y a quelque chose qui contient une fraction, un branchement conditionnel est effectué et la valeur de la fraction est conservée. Mis en œuvre en ajoutant dans le calcul final Il y a beaucoup de déclarations if ...
Recommended Posts