Add code to the following script so that it uses the built-in zip function to find and print the average speed in miles/hr for five errands based on distance and time data collected by a GPS unit. The distance for each trip is given in miles in distanceList and the corresponding times are given in minutes and seconds in the same order in timeList. The time format separates minutes from seconds with a colon. Use string methods and built-in functions inside the zip loop to extract the numeric temporal values.
Script:
distanceList = [0.04, 0.05, 0.91, 0.16, 18]
timeList = ['7m:13s', '11m:29s', '16m:48s', '3m:26s',
'120m:0s']
###Hint 1: Inside the 'zip' loop, use several steps to extract
the minutes and seconds
### and then convert to hours.
###Hint 2: To print a float with 2 or less decimal places, put
':.2f' inside the format place holder.
### Example,
### >>> speed = 5.1666666666667
### >>> print 'Speed: {0:.2f}
miles/hr'.format(speed)
### Speed: 5.17 miles/hr
The output should look like this:
>>>Distance: 0.04 Time: 7m:13s Speed: 0.33
miles/hr
Program:
distanceList = [0.04, 0.05, 0.91, 0.16, 18]
timeList = ['7m:13s', '11m:29s', '16m:48s', '3m:26s', '120m:0s']
# zip loop
for (a, b) in zip(distanceList, timeList):
# lines 7 and 8 are used to extract the minutes
c = b.split('m')
minutes = int(c[0])
# lines 11 to 13 are used to extract the seconds
d = c[1].split(':')
e = d[1].split('s')
seconds = int(e[0])
# conversion of time to hours
hours = (seconds + minutes*60)/3600
speed = a/hours
# output the desired result
print('Distance: ' + str(a) + ' Time: ' + b +
' Speed: {0:.2f} miles/hr'.format(speed))
Note: Please refer to the screenshot of the code to understand the indentation of the code.
Screenshot of the code:
Output:
Add code to the following script so that it uses the built-in zip function to find...