Question: I implemented the code below but it is failing the test case and getting the following error The call past_a_week(datetime.date(2019, 10, 12), datetime.date(2019, 10, 19))
I implemented the code below but it is failing the test case and getting the following error
The call past_a_week(datetime.date(2019, 10, 12), datetime.date(2019, 10, 19)) returns False, not True.
I'm not sure how to fix my code. I can only use the imported library date time
import datetime
def past_a_week(d1,d2): """ Returns True if event d2 happens at least a week (7 days) after d1.
If d1 is after d2, or less than a week has passed, this function returns False. Values d1 and d2 can EITHER be date objects or datetime objects.If a date object, assume that it happens at midnight of that day.
Parameter d1: The first event Precondition: d1 is EITHER a date objects or a datetime object Parameter d2: The first event Precondition: d2 is EITHER a date objects or a datetime object """
# HINT: Check the type of d1 or d2. If not a datetime, convert it for comparison
if isinstance(d1, datetime.datetime): isvd1 = True newD1 = d1 else: isvd1 = True try: newD1 = datetime.date(d1.year, d1.month, d1.day) except ValueError: isvd1 = False
if isinstance(d2, datetime.datetime): isvd2 = True newD2 = d2 else: isvd2 = True try: newD2 = datetime.date(d2.year, d2.month, d2.day) except ValueError: isvd2 = False
if isvd1 and isvd2: difference = newD2-newD1 if difference.total_seconds()/86400 > 7 : return True else: return False
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
