From the course: Python Code Challenges for Object-Oriented Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Subtracting months

Solution: Subtracting months

- [Instructor] Let's go ahead and take a look at our test cases. So in the first one we're subtracting a month offset of five from a month offset of three, and we get a month offset of two. So let's go ahead and just select Test my code. And it's not surprising that we have a type error, unsupported operand, we're trying to use a subtract between a month offset and a month offset, and we haven't created the code for that yet. So let's go over to month offset. And now fortunately for us, we've done a lot of the work with the add, and so let's use that as our starting point. And so I need to replace the dunder add with the dunder sub. The instance is going to be MonthOffset, and then all I need to do is replace the plus with a minus. So let's go ahead and test that out, and you can see that that's passed our first test case. Now let's head over to the second one, which is where we're going to be subtracting a month from a month and we will get a month offset. So I'm going to head up…

Contents