Skip to content Skip to sidebar Skip to footer

Python - Add Days To An Existing Date

Obviously this is homework so I can't import but I also don't expect to be spoon fed the answer. Just need some help on something that's probably pretty simple, but has had me stum

Solution 1:

It's running the correct number of times, but since nextday doesn't mutate a date object, you are just asking for the date after the current one over and over. Try:

def__add__(1, n):
    """
    Returns a new date after n days are added to given date.
    """
    g = self.nextday()
    for x inrange(1, n):
        g = g.nextday()
    return g

With g = self.nextday(), you create a temporary object that is then incremented by repeatedly assigning itself to the following day. The range has to be changes to range(1,n) to compensate for the initial day, though I'd personally write it as range(0,n-1).

Post a Comment for "Python - Add Days To An Existing Date"