我的代码如下 - 是的,丑陋的。我正在学习python。它适用于开始/结束日期都是同一天的情况。
当start = 2017,6,15 6:00&结束= 2017,6,16 6:00 应用的if语句是start<早上6点。有人可以解释原因吗?它确实认识到1天是持续时间。
从datetime导入日期时间,日期,时间
def babysitting():
(st_yr,st_mon,st_day) = [int(s) for s in input("Enter the start date in the format: YYYY,M,D: ").split(',')]
(st_hr,st_min) = [int(s) for s in input("Enter the start time in 24hr format -> HH:MM: ").split(":")]
(e_yr,e_mon,e_day) = [int(s) for s in input("Enter the end date in the format: YYYY,M,D: ").split(',')]
(e_hr,e_min) = [int(s) for s in input("Enter the end time in 24hr format -> HH:MM: ").split(":")]
st_date = date(st_yr,st_mon,st_day)
st_time = time(st_hr,st_min)
end_date= date(e_yr,e_mon,e_day)
end_time = time(e_hr,e_min)
start = datetime.combine(st_date,st_time)
end = datetime.combine(end_date,end_time)
ninePM = datetime.combine(end_date, time(21,00))
sixAM = datetime.combine(end_date, time(6,00))
if start >= sixAM:
if end <= ninePM:
normal_rate_duration = end - start
normal_rate_seconds = normal_rate_duration.total_seconds()
normal_rate_billing = round(((normal_rate_seconds / 60) / 60) * 2.5,2)
print("Cost for babysitting is: $", normal_rate_billing)
elif end > ninePM:
normal_rate_duration = ninePM - start
reduced_rate_pm_duration = end - ninePM
normal_rate_seconds = normal_rate_duration.total_seconds()
normal_rate_billing = round(((normal_rate_seconds / 60) / 60) * 2.5,2)
reduced_rate_pm_seconds = reduced_rate_pm_duration.total_seconds()
reduced_rate_pm_billing = round(((reduced_rate_pm_seconds / 60) / 60) * 1.75,2)
print("Cost for babysitting is: $", normal_rate_billing + reduced_rate_pm_billing)
if start < sixAM:
if end <= ninePM:
reduced_rate_am_duration = sixAM - start
reduced_rate_am_seconds = reduced_rate_am_duration.total_seconds()
reduced_rate_am_billing = round(((reduced_rate_am_seconds / 60) / 60) * 1.75,2)
normal_rate_duration = end - sixAM
normal_rate_seconds = normal_rate_duration.total_seconds()
normal_rate_billing = round(((normal_rate_seconds / 60) / 60) * 2.5,2)
print("Cost for babysitting is: $", normal_rate_billing + reduced_rate_am_billing)
elif end > ninePM:
reduced_rate_am_duration = sixAM - start
reduced_rate_am_seconds = reduced_rate_am_duration.total_seconds()
reduced_rate_am_billing = round(((reduced_rate_am_seconds / 60) / 60) * 1.75,2)
normal_rate_duration = ninePM - sixAM
normal_rate_seconds = normal_rate_duration.total_seconds()
normal_rate_billing = round(((normal_rate_seconds / 60) / 60) * 2.5,2)
reduced_rate_pm_duration = end - ninePM
reduced_rate_pm_seconds = reduced_rate_pm_duration.total_seconds()
reduced_rate_pm_billing =round(((reduced_rate_pm_seconds / 60) / 60) * 1.75,2)
print("Cost for babysitting is: $", normal_rate_billing + reduced_rate_am_billing + reduced_rate_pm_billing)
babysitting()
答案 0 :(得分:1)
你的if
检查开始时间是否早于结束日期的早上6点,这当然是(前一天的某个时间或类似的事情)。您需要检查st_time
是否早于早上6点(无论日期或st_date
日期)。