Python 3.x 调试日期间隔天数的函数 #来自论坛的积分归Websten所有 # #利用Dave的建议在两个日期之间完成一天 #程序。它需要考虑闰年 #除了每月的正确天数之外。 def daysInMonth(年、月): 如果月份在(1,3,5,7,8,10,12)中: 返回31 (4,6,9,11)中的elif月: 返回30 elif月==2: 如果第%4年==0: 返回29 其他: 返回28 def下一天(年、月、日): “”“简单版本:假设每个月有30天”“” 如果天

Python 3.x 调试日期间隔天数的函数 #来自论坛的积分归Websten所有 # #利用Dave的建议在两个日期之间完成一天 #程序。它需要考虑闰年 #除了每月的正确天数之外。 def daysInMonth(年、月): 如果月份在(1,3,5,7,8,10,12)中: 返回31 (4,6,9,11)中的elif月: 返回30 elif月==2: 如果第%4年==0: 返回29 其他: 返回28 def下一天(年、月、日): “”“简单版本:假设每个月有30天”“” 如果天,python-3.x,algorithm,Python 3.x,Algorithm,问题是,在最后一个案例中,它没有显示正确的答案,我的头脑正在翻转,我无法找到问题所在,我多次更改代码,但问题仍然存在 有人能一步一步地指导我解决这个问题吗? 我是初学者 我想最后一个测试案例可能有一个输入错误,答案是再过一天:36524。当你把它作为最后一个参数时,它表明它通过了。请让我知道您是否完整,确保您给出的答案是正确的。如果我们不包括最晚的日期,这就是正确的答案我不理解您的答案。“迟到”是什么意思?对不起,包括上次约会是的,这就是问题所在。最后一个日期,一天后是正确的。你的代码没有错,最

问题是,在最后一个案例中,它没有显示正确的答案,我的头脑正在翻转,我无法找到问题所在,我多次更改代码,但问题仍然存在

有人能一步一步地指导我解决这个问题吗?

我是初学者

我想最后一个测试案例可能有一个输入错误,答案是再过一天:36524。当你把它作为最后一个参数时,它表明它通过了。请让我知道您是否完整,确保您给出的答案是正确的。

如果我们不包括最晚的日期,这就是正确的答案我不理解您的答案。“迟到”是什么意思?对不起,包括上次约会是的,这就是问题所在。最后一个日期,一天后是正确的。你的代码没有错,最后一个答案是错的
# Credit goes to Websten from forums
#
# Use Dave's suggestions to finish your daysBetweenDates
# procedure. It will need to take into account leap years
# in addition to the correct number of days in each month.
def daysInMonth(year,month):
    if month in (1,3,5,7,8,10,12):
        return 31
    elif month in (4,6,9,11):
        return 30
    elif month == 2:
        if year%4 ==0:
            return 29
        else:
            return 28

def nextDay(year, month, day):
    """Simple version: assume every month has 30 days"""
    if day < daysInMonth(year,month):
        return year, month, day + 1
    else:
        if month == 12:
            return year + 1, 1, 1
        else:
            return year, month + 1, 1

def dateIsBefore(year1, month1, day1, year2, month2, day2):
    """Returns True if year1-month1-day1 is before year2-month2-day2. Otherwise, returns False."""
    if year1 < year2:
        return True
    if year1 == year2:
        if month1 < month2:
            return True
        if month1 == month2:
            return day1 < day2
    return False        

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    """Returns the number of days between year1/month1/day1
       and year2/month2/day2. Assumes inputs are valid dates
       in Gregorian calendar."""
    # program defensively! Add an assertion if the input is not valid!
    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

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()