In: Computer Science
Python
While traveling home for the holiday, you wondered how much time you'd save if you drove faster. The distance home is 120 miles, and you decided to compare driving 55 mph versus 70 mph. You also wondered if driving faster around town really saves you that much time. The around-town distance you chose was 5 miles, using speeds of 25 and 35 mph. Of course, you decided that no amount of times savings is worth the risk to yourself or others, but you still wanted to find the answer.
Create two functions to help calculate the travel time in hours, minutes and seconds and to display the results.
# Calculate travel time in minutes given the distance in miles and the speed in mph calc_travel_time ( distance, speed ) # Output the travel time hours, minutes and seconds given distance and speed print_travel_time ( distance, speed )
Your Python solution must include the following:
Tip:
Expected output:
To travel 120 miles at 55 MPH will take 2 hr, 10 min and 55 sec To travel 120 miles at 70 MPH will take 1 hr, 42 min and 51 sec To travel 5 miles at 25 MPH will take 0 hr, 12 min and 0 sec To travel 5 miles at 35 MPH will take 0 hr, 8 min and 34 sec
Given below is the code for the question. Ensure to indent as shown in image. Please do rate the answer if it helped. Thank you
# Calculate travel time in minutes given the distance in miles
and the speed in mph
def calc_travel_time( distance, speed ):
t = distance/speed
t = t * 60 #convert to minutes
return t
# Output the travel time hours, minutes and seconds given
distance and speed
def print_travel_time( distance, speed ):
t = calc_travel_time(distance, speed)
mins = int(t)
secs = int(round(60*(t - mins), 0))
hours = mins // 60
mins = mins % 60
print('To travel {} miles at {} MPH will take {} hr,
{} min and {} sec'.format(distance, speed, hours, mins, secs))
print_travel_time(120, 55)
print_travel_time(120, 70)
print_travel_time(5, 25)
print_travel_time(5, 35)