代码之家  ›  专栏  ›  技术社区  ›  beginner_geek07

如何在不使用datetime模块的情况下计算给定年份的剩余天数?

  •  0
  • beginner_geek07  · 技术社区  · 2 年前

    我一直在努力计算一年中剩余的天数。

    我已经定义了一个函数,用于确定一年是否为闰年,以及给定月份的天数。但是,我在定义最后一个函数时遇到了一个难题,没有使用datetime模块。

    year = int(input("Enter the year to determine the number of days: "))
    month = int(input("Enter the month of the year: "))
    day = int(input("Enter the day of the year: "))
    
    def if_leap_year(year):
    
        if (year % 400 == 0): return 366
        
        elif (year % 100 == 0): return 365
        
        elif (year % 4 == 0): return 366
    
        else:
            return 365
    
    print(if_leap_year(year))
    
    def days_in_month(month, year):
        if month in {1, 3, 5, 7, 8, 10, 12}:
            return 31
        if month == 2:
            if if_leap_year(year):
                return 29
            return 28
        return 30
    
    print(days_in_month(month, year))
    
    
    def days_left_in_year(month, day, year):
        month2 = 12
        day2 = 31
        day = day2 -365
    

    我被困在最后一部分。。。任何帮助都将不胜感激。

    编辑: 现在我有:

        def days_left_in_year(month, day, year):
            daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
            daysLeft = (if_leap_year(year) if month < 3 else 365) - sum(daysInMonth[:month - 1]) - day
            return daysLeft
    

    我想使用这三个函数来创建类似以下内容的输出:

    你什么时候取消了次贷服务?

    月份:2

    日期:29

    年份:2021

    抱歉,2021年2月29日似乎不是有效日期。要不要再试一次?

    月份:2

    日期:29

    年份:2020年

    我试着使用:

    def refund_period():
        year = int(input("Enter the year to determine the number of days: "))
        month = int(input("Enter the month of the year: "))
        day = int(input("Enter the day of the year: "))
        if if_leap_year(year) == 365:
            money_owed = (days_left_in_year(month, day, year) / 365) * 278
        if if_leap_year(year) == 366:
            money_owed = (days_left_in_year(month, day, year) / 366) * 278
    

    但是欠的钱没有打电话,所以现在我又被卡住了(

    1 回复  |  直到 2 年前
        1
  •  1
  •   constantstranger    2 年前

    下面是一个经过最小修改的代码版本,可以满足您的要求:

    year = int(input("Enter the year to determine the number of days: "))
    #month = int(input("Enter the month of the year: "))
    #day = int(input("Enter the day of the year: "))
    
    def if_leap_year(year):
    
        if (year % 400 == 0): return 366
        
        elif (year % 100 == 0): return 365
        
        elif (year % 4 == 0): return 366
    
        else:
            return 365
    
    #print(if_leap_year(year))
    
    def days_in_month(month, year):
        if month in {1, 3, 5, 7, 8, 10, 12}:
            return 31
        if month == 2:
            if if_leap_year(year):
                return 29
            return 28
        return 30
    
    #print(days_in_month(month, year))
    
    def days_left_in_year(month, day, year):
        daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
        daysLeft = (if_leap_year(year) if month < 3 else 365) - sum(daysInMonth[:month - 1]) - day
        return daysLeft
    
    print(days_left_in_year(1,1,2020))
    print(days_left_in_year(3,1,2020))
    print(days_left_in_year(1,1,2022))
    print(days_left_in_year(3,1,2022))
    

    输出:

    365
    305
    364
    305
    

    更新: 我想你只是想回答问题的第二部分:

    def refund_period():
        year = int(input("Enter the year to determine the number of days: "))
        month = int(input("Enter the month of the year: "))
        day = int(input("Enter the day of the year: "))
        if if_leap_year(year) == 365:
            money_owed = (days_left_in_year(month, day, year) / 365) * 278
        if if_leap_year(year) == 366:
            money_owed = (days_left_in_year(month, day, year) / 366) * 278
        return money_owed
    
    print(refund_period())
    

    你的职能 refund_period() 没有归还任何东西,所以我补充说 return money_owed ,然后我加了一行 print(refund_period()) .

    下面是一个运行示例:

    Enter the year to determine the number of days: 2020
    Enter the month of the year: 02
    Enter the day of the year: 29
    232.42622950819674