计算指定的年月日是这一年的第几天

def is_leap_year(year):
    return  year % 4 == 0 and year % 100 != 0 or year % 400 == 0

def which_day(year,month,date):
    days_of_which = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]][is_leap_year(year)]
    total = 0
    for index in range(month - 1):
        total += days_of_which[index]
    return total + date

def main():
    print(which_day(1980,11,28))
    print(which_day(1981, 11, 28))
    print(which_day(2018, 1, 28))
    print(which_day(2016, 3, 1))

if __name__ == '__main__':
    main()

你可能感兴趣的:(简单算法,python)