average time calculation??
Hi there guys i've got a script that's suppose to find the average of two times
as strings. The times are in minutes:seconds:milliseconds
i'm doing ok in printing the right minutes and seconds my problem is with the
milliseconds.
Example if i have 00:02:20 and 00:04:40 the average will be 00:03:30 or
00:02:00 and 00:03:00 will be 00:02:30
Can anyone help me out with this please. Here is the code that i have so far:
def lap_average(lap1, lap2):
t1 = lap1.replace(":",'')
t2 = lap2.replace(":",'')
mins1, secs1, hundreths1 = t1[:2], t1[2:4], t1[4:]
mins2, secs2, hundreths2 = t2[:2], t2[2:4], t2[4:]
total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60
millisec = (total_seconds * 1000)
millisec = millisec / 2
micro_x = millisec
minutes = micro_x / (60*1000)
micro_x = micro_x - minutes * (60*1000)
seconds = micro_x / 1000
micro_x = micro_x - seconds
print '%02d:%02d:%s' % (minutes, seconds, micro_x)
lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00')
lap_average('00:02:20', '00:04:40')
lap_average('02:40:40', '03:30:30')
lap_average('02:60:30', '60:40:40')
Thanks in Advance
--
http://mail.python.org/mailman/listinfo/python-list
Re: average time calculation??
Hi Oscar, Thank you for your reply, and you are absolutely right, I meant hundredths of a second to be outputed -- http://mail.python.org/mailman/listinfo/python-list
Re: average time calculation??
Hi Oscar, again I do apologize for my beginner mistakes, I've changed the code
taking in consideration some of your and MRAB suggestions.
Could you give me an example on how could I use the datetime.timedelta function
in this particular case.
This is my code:
def lap_average(lap1, lap2):
mins1, secs1, hundreths1 = lap1.split(":")
mins2, secs2, hundreths2 = lap2.split(":")
minutes = int(mins1) + int(mins2)
seconds = float(secs1) + float(secs2)
hundredths = int(6 * minutes + 1000 * seconds)
hundredths = hundredths // 2
print hundredths
lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00') #should output: 00:02:50
lap_average('00:02:20', '00:04:40') # 00:03:30
lap_average('02:40:40', '03:30:30') # etc
lap_average('02:60:30', '60:40:40')
Also I was a bit confused with what you said about :
"> total_seconds = int(secs1) + int(secs2) + int(mins1) * 60 + int(mins2) * 60
What happened to the hundredths in the line above. Surely you wanted
to add 0.01 * hundredths there."
I thought the above was already the entire time as hundredths of second??
--
http://mail.python.org/mailman/listinfo/python-list
