Question
Getting following error and unsure how to fix it line 46, in past_a_week difference = d2 -d1 TypeError: unsupported operand type(s) for -: 'datetime.datetime' and
Getting following error and unsure how to fix it
line 46, in past_a_week difference = d2 -d1 TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'datetime.date'
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.datetime(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.datetime(d2.year, d2.month, d2.day) except ValueError: isvd2 = False
if isvd1 and isvd2: difference = d2 - d1
if difference.total_seconds()/86400 >= 7 : return True else: return False
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started