输入年月日,计算距1949年10月1日的天数

def is_leap_year(year):

    if (year%4==0 and year%100!=0) or year%400==0:

        return True

    else:

        return False


def count_days_interval():

    input_date=input(

"请输入日期,格式:year-month-day:")

    if input_date.count("-")!=2:

        print(

"输入日期格式有误,格式:year-month-day,请重新输入日期")

        return None

    year,month,day=input_date.split("-")

    if not (year.isdigit() and month.isdigit() and day.isdigit()):

        print(

"输入日期必须为数字,格式:year-month-day,请重新输入日期:")

    year=int(year)

    month=int(month)

    day=int(day)

    init_year = 1949

    init_month = 10

    init_day = 1

    if year-init_year==0:

        if month ==init_month:

            return day -init_day

        elif month -init_month==1:

            return 31+day-1

        else:

            return 31+30+day-1

    elif year-init_year==1:

        month_days=[31,28,31,30,31,30,31,31,30,31,30,31]

        leap_day=0

        if month >2 and is_leap_year(year):

            leap_day=1

        days =0

        for i in month_days[:month-1]:

            days+=i

        return 92+leap_day+days+day

    else:

        month_days=[31,28,31,30,31,30,31,31,30,31,30,31]

        leap_day=0

        if month >2 and is_leap_year(year):

            leap_day=1

        days =0

        for i in month_days[:month-1]:

            days+=i

        leap_days=0

        for i in range(1950,year):

            if is_leap_year(i):

                leap_days+=1

        years = year -init_year-1

        return 92+leap_day+days+day+years*365+leap_days-1



print(count_days_interval())

你可能感兴趣的:(输入年月日,计算距1949年10月1日的天数)