In: Computer Science
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code.
#!/usr/bin/env python3
#Arrival Date/Time Estimator
#
#
from datetime import datetime
import locale
mph = 0
miles = 0
def get_departure_time():
while True:
date_str =
input("Estimated time of departure (HH:MM AM/PM): ")
try:
depart_time = datetime.strptime(date_str, "%H:%M %p")
except ValueError:
print("Invalid date format. Try again.")
continue
return depart_time
def get_departure_date():
while True:
date_str =
input("Estimated date of departure (YYYY-MM-DD): ")
try:
depart_date = datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
print("Invalid date format. Try again. ")
continue
return depart_date
def travel_calculations():
Estimated_travel_time = (miles /
mph)
a=timedelta(hours=Estimated_travel_time)
a=str(a)
a=a.split(':')
print (" The estimated travel time:
",a[0],'hours and ', a[1],'minutes')
def main():
print("Arrival Time Estimator\n")
depart_time = get_departure_time()
depart_date = get_departure_date()
travel_calculations()
miles = input("Enter miles: ")
mph = input("Enter miles per hour : ")
return
if __name__ == "__main__":
main()
Created Functions distance and speed functions to get input from users along with little error handling.
And return distance and speed and store these values to miles, mph
Then pass these variables as parameters to our travel_calculations(miles, mph). In order to calculate total hours and minutes
Python program
from datetime import datetime,timedelta mph = 0
def distance(): try:
if __name__ == "__main__": |
Output