我需要一个有关python程序的帮助,该程序可以计算旅行的估计时数和分钟数。这应该包括估计的出发日期/时间和估计的到达日期/时间。但是,我可以显示以下内容: 预计出发日期(YYYY-MM-DD):2018-11-29预计出发时间(HH:MM AM / PM):11:15 PM 输入里程:500 输入每小时的英里数:80
预计旅行时间 营业时间:6 分钟:15.0 预计抵达日期:2018-11-30 预计到达时间:05:30 AM
继续吗? (y / n):n
from datetime import datetime,timedelta
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 distance():
while True:
miles = input("Enter miles to be travelled: ")
try:
miles = int(miles)
if miles <= 0:
print("Enter a Number Greater than 0.\n")
else:
return miles
except ValueError:
print("Format Error . Please Try again\n")
continue
def speed():
while True:
speed = input("Enter miles per hour: ")
try:
mph = int(speed)
if mph <= 0:
print("Enter a Number Greater than 0.\n")
else:
return mph
except ValueError:
print("Format Error . Please Try again\n")
continue
pass
def travel_calculations(miles,mph):
hours = int(miles/mph)
minutes = int(((miles/mph)-hours)*60)
time_travelled = timedelta(hours=hours, minutes=minutes)
print("\n\nEstimated travel time")
print("Hours: ", hours)
print("Minutes: ", minutes)
def main():
print("Arrival Time Estimator\n")
depart_time = get_departure_time()
depart_date = get_departure_date()
miles = distance()
mph = speed()
travel_calculations(miles,mph)
return
if __name__ == "__main__":
main()
在此处输入代码