Infinite Loop during a date counter

时间:2016-12-09 12:43:44

标签: python

I have searched and found someone with an identical problem found here: Python daysBetweenDate

However, their code is different than mine, and mine appears to address the issues that were pointed out in the answers for that post.

In my code, I don't understand why I am running into an infinite loop.

def leap_year_check(year):
    if (year%4==0 and year%100==0 and year%400==0) or (year%4==0 and year%100!=0):
        return True
    else:
        return False
    #TESTED-leap_year_check-PASSED

def nextDay(year, month, day):
    if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 and day < 31:
        return year, month, day + 1
    if month == 4 or month == 6 or month == 9 or month == 11 and day < 30:
        return year, month, day + 1
    if month == 2 and day <28:
        return year, month, day + 1
    if month == 2 and leap_year_check(year)==True and day==28:
        return year, month, day + 1
    else:
        if month == 12:
            return year + 1, 1, 1
        else:
            return year, month + 1, 1
    #TESTED nextDay - PASSED

def dateIsBefore(year1, month1, day1, year2, month2, day2):
    if year1 < year2:
        return True
    if year1 == year2:
        if month1 < month2:
            return True
        if month1 == month2:
            return day1 < day2
    return False        
    #TESTED dateIsBefore - PASSED

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    assert not dateIsBefore(year2, month2, day2, year1, month1, day1)
    days = 0
    while dateIsBefore(year1, month1, day1, year2, month2, day2):
        year1, month1, day1 = nextDay(year1, month1, day1)
        days += 1
    return days
    #***Keep running into infinite loop when testing daysBetweenDates
    #ALL other functions operate correctly when tested on their own
    #ALL other functions work together EXCEPT when running daysBetweenDates

def test():
    test_cases = [((2012,1,1,2012,2,28), 58), 
                  ((2012,1,1,2012,3,1), 60),
                  ((2011,6,30,2012,6,30), 366),
                  ((2011,1,1,2012,8,8), 585 ),
                  ((1900,1,1,1999,12,31), 36523)]

    for (args, answer) in test_cases:
        result = daysBetweenDates(*args)
        if result != answer:
            print "Test with data:", args, "failed"
        else:
            print "Test case passed!"

test()

As far as I can understand, daysBetweenDates is running the nextDay function and increments +1 to days, as long as dateIsBefore returns False, which should then add up to the number of days in between when dateIsBefore becomes True.

I am obviously missing something.

I understand that the way I am going about this is likely incredibly inefficient, but it is for learning purposes and I am definitely not yet at the point in which I need to focus on optimization. Right now I am just trying to understand how to make the code work.

Any help would be greatly appreciated.

1 个答案:

答案 0 :(得分:2)

Part of learning to code is learning to debug. Try putting a print statement that shows the current values of year1, month1, day1 within your while loop inside daysBetweenDates and you will see what happens: nextDay just increments the day constantly, never incrementing the month or year.