In: Computer Science
Need the following program in Python:
Create a program that calculates the estimated duration of a trip in hours and minutes. This should include an estimated date/time of departure and an estimated date/time of arrival.
Arrival Time Estimator Enter date of departure (YYYY-MM-DD): 2016-11-23 Enter time of departure (HH:MM AM/PM): 10:30 AM Enter miles: 200 Enter miles per hour: 65 Estimated travel time Hours: 3 Minutes: 5 Estimated date of arrival: 2016-11-23 Estimated time of arrival: 01:35 PM Continue? (y/n): y Enter date of departure (YYYY-MM-DD): 2016-11-29 Enter time of departure (HH:MM AM/PM): 11:15 PM Enter miles: 500 Enter miles per hour: 80 Estimated travel time Hours: 6 Minutes: 20 Estimated date of arrival: 2016-11-30 Estimated time of arrival: 05:35 AM Continue? (y/n): n Bye! |
Specifications
Please find the Code as below:
#Importing the datetime classes
import datetime
from datetime import date, time
from datetime import datetime
from datetime import timedelta
print("Arrival Time Estimator")
dep_dt = input("Enter date of departure (YYYY-MM-DD): ")
dep_time = input("Enter time of departure (HH:MM AM/PM): ")
#Combine String to get the Departure Date and Time
departure_date = dep_dt + " " + dep_time
#Use of strptime method to convert the 'departure_date' string into Date object with 12 Hr format
dep_date_obj = datetime.strptime(departure_date, '%Y-%m-%d %I:%M %p')
miles = int(input("Enter miles: "))
miles_ph = int(input("Enter miles per hour: "))
print("Estimated travel time")
#Calculating 'Hours' and 'Minutes' using the 'miles' and 'miles per hour' values
hours = miles//miles_ph
minutes = round(((miles/miles_ph)*60)%60)
print("Hours : ", hours)
print("Minutes : ", minutes)
#Adding the calculated Hours and Minutes with the dep_date_obj using the in-built function timedelta
arr_dt_obj = dep_date_obj + timedelta(hours=hours,minutes=minutes)
#Converting the arr_dt_obj to string using strftime method in 12 Hr format
arrival_date = datetime.strftime(arr_dt_obj, '%Y-%m-%d %I:%M %p')
print("Estimated date of arrival: ", arrival_date[:-8])
print("Estimated time of arrival: ", arrival_date[11:])
Please refer the below code screenshot for the better understanding of Indentations:
Sample Output: